This article has participated in the good Article call order activity, click to view:Back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!

Regular for most front-end is a headache, learn, the actual use is not much, and time consuming, Baidu CV, and can not find the appropriate regular, this monkey sorted out the commonly used regular, you objective three keys together, convenient to find 😀.

Daily use

1. Check whether the phone number is correct

/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
Copy the code

2. Check whether it is an email address

/ ^ (| [a zA - Z] [0-9]) | \ (\ w) + @ [a - zA - Z0-9] + \. ([a zA - Z] {2, 4}) $/Copy the code
  1. Check whether the URL is valid
/^(https? |ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?) (\. (25 [0 to 5] | 2 [0 to 4] [0-9] [0-9] {2} | 1 | [1-9]? [0-9])){3}|([a-zA-Z0-9-]+\.) *[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA- Z0-9.,?'\\+&%$#=~_-]+))*$/Copy the code

4. Domestic landline number

/\d{3}-\d{8}|\d{4}-\d{7}/
Copy the code

5. The value must contain the combination of uppercase and lowercase letters and digits and cannot contain special characters. The value must be between N and M in length

const checkNotSpecial = (options) => new RegExp('^(? =.*\\d)(? =.*[a-z])(? =.*[A-Z]).{'+ options.start +','+ options.end + '}$').test(options.val)Copy the code

6. HTML tags

/<(\\S*?) [^ >] * >. *? < 1 > / \ \ | <. *? />/Copy the code

7. China Postal Code

/[1-9]\d{5}(? ! \d)/Copy the code

8. The IP address

/^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.( 1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$/Copy the code

9. The port

| / ^ (([0-9] [1-9] \ d {1, 3} | [1-5] \ d {4} | 6 [0 to 5] {2} [0, 3] [0-5])) $/Copy the code
  1. Numbers, upper and lower case letters, special characters
/ ^ (? = (. *? [a-z])(? = (. *? [A-Z])(? =.*\d)(? =. * [~! @ # $% ^ & * () _ + ` \ = {} : "' < >?,.. \]) /Copy the code
  1. Numbers, special characters
/ ^ (? =.*\d)(? =. * [~! @ # $% ^ & * () _ + ` \ = {} : "' < >?,.. \]) /Copy the code
  1. Upper and lower case letters, special characters
/ ^ (? = (. *? [a-z])(? = (. *? [A-Z])(? =. * [~! @ # $% ^ & * () _ + ` \ = {} : "' < >?,.. \]) /Copy the code
  1. Numbers and upper and lower case letters
/ ^ (? = (. *? [a-z])(? = (. *? [A-Z])(? =.*\d)/Copy the code
  1. Special characters
/ (? =. * [~! @ # $% ^ & * () _ + ` \ = {} : "' < >?,.. \]) /Copy the code

String check

  1. The date of validation
/ ^ \ d {4} \ d {1, 2} - \ d {1, 2} /Copy the code
  1. Lowercase letters
/^[a-z]+$/
Copy the code
  1. The capital letters
/^[A-Z]+$/
Copy the code
  1. Upper and lower case
/^[A-Za-z]+$/
Copy the code
  1. Check id card
/^\d{15}|\d{18}$/
Copy the code
  1. The name must start with a letter and contain n to M letters, digits, and underscores (_)
const checkPasswordSpecification = (options) => new RegExp('^[a-zA-Z]\\w{'+ options.start +','+ options.end +'}$').test(options.val)
Copy the code
  1. Chinese characters
/^[\u4e00-\u9fa5]*$/
Copy the code

8. A string of 26 letters, digits, or underscores (_)

/^\w+$/
Copy the code
  1. Chinese, English, and numbers including underscores
/^[\u4E00-\u9FA5A-Za-z0-9_]+$/
Copy the code

10. Chinese, English and numbers but not including underscores

 /^[\u4E00-\u9FA5A-Za-z0-9]+$/
Copy the code

Digital calibration

  1. Verify that the string is a number
/ ^ [0-9] +.? [0-9] * $/Copy the code
  1. Detect N bits
const checkNumberLength = (options) => new RegExp("^\\d{" + options.length + "}$").test(options.val)
Copy the code
  1. Detect m- N digits
const checkAppointLen: = (options) => new RegExp("^\\d{" + options.start + ',' +  options.end + "}$").test(options.val)

Copy the code

4. Numbers beginning with zero and non-zero

/ ^ (0 | [1-9] [0-9] *) $/Copy the code

5. Non-zero digits with at most two decimal digits

/ ^ ((1-9] [0-9] *) + (. [0-9] {1, 2})? $/Copy the code

6. Positive or negative numbers with 1-2 decimal places

/ ^ (\ -)? \ d + (\ \ d {1, 2})? $/Copy the code

7. Positive, negative, and decimal numbers

/ ^ (\ | \ +)? \d+(\.\d+)? $/Copy the code

8. A positive real number with two decimal places

/ ^ [0-9] + (. [0-9] {2})? $/Copy the code

9. Positive real numbers with 1 to 3 decimal places

/ ^ [0-9] + (. [0-9] {1, 3})? $/Copy the code

10. Positive non-zero integers

/^[1-9]\d*/
Copy the code

11. Non-zero negative integers

/^-[1-9]\d*$/
Copy the code

12. Non-negative integers

/^\d+$/
Copy the code

13. Non-positive integers

/^-[1-9]\d*|0$/
Copy the code

14. Non-negative floating-point numbers

/^\d+(\.\d+)? $/Copy the code

15. Non-positive floating point numbers

/^((-\d+(\.\d+)?) | (0 + (\. 0 +)? ) $/Copy the code

16. Positive floating point numbers

/^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$/
Copy the code

17. Negative floating point number

/^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$/
Copy the code

18. The floating point number

/ ^ (-? \d+)(\.\d+)? $/Copy the code