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

Form validation can’t run away, regular expression you need!

When using regular expressions, you always forget what the symbols stand for!

Friends, since come in, then have a look!

1. Common characters

  • /[abc]/g

[] : match […] For example, [ABC] matches all a, B, and C characters in the string “China”.

  • /[^abc]/g

[^] : Match other than […] For example, [^ ABC] matches all the characters except a, B, and C in the string “China”.

  • /[A-Z]/g

[A-z] indicates A range that matches all uppercase letters, and [A-z] indicates all lowercase letters.

  • /./g

. : Matches any single character except the newline characters (\n, \r), equal to [^\n\r].

  • /[\s\S]/g

[\s\ s] : matches all. \s matches all whitespace, including newlines, \s non-whitespace, excluding newlines.

  • /\w/g

\w: Matches letters, digits, and underscores. Equivalent to [A Za – z0-9 _]

2. Non-printing characters

\n: Matches a newline character.

\r: Matches a carriage return character.

\s: Matches any whitespace character, including Spaces, feed characters, and so on.

\S: Matches any non-whitespace character.

3. Special characters

$: matches the end of the input string.

Example: /b$/g matches the character ‘abcdab’ to get the last b

() : Marks the start and end positions of a subexpression.

Example: /(ab)*/g, matches the character ‘abcdab’, and gets ab twice

* : matches the previous subexpression zero or more times.

Example: /ab*/g, matches the character ‘abcdab’, and gets ab twice

+ : matches the previous subexpression one or more times.

Example: /ab+/g, matches the character ‘abcdab’, and gets ab twice

. : Matches any single character except the newline \n.

Example: /./g, matches the character ‘abcd’ to get a, b, C, d

[: Marks the beginning of a bracket expression.

Example: /[a]/g, matches the character ‘abcd’, gets an A

? : matches the preceding subexpression zero or one times, or indicates a non-greedy qualifier.

Example: / ab (CD)? /g, match the characters ‘abcd’, ‘ab’, and get abcd, ab, just like a fuzzy query

\ : Marks the next character as either a special character, or a literal character, or a backreference, or an octal escape.

Example: /\^/g, matches the character ‘ab^ CD ‘, and gets ^

^ : matches the starting position of the input string unless used in a square bracket expression, when used in a square bracket expression, it indicates that the set of characters in that square bracket expression is not accepted.

Example: /^[a]/g, matches the character ‘abcd’, gets an A

{: Marks the beginning of a qualifier expression.

Example: /{a}/g, matches the character ‘abcd’, gets an A

| : indicate a choice between the two.

Example: / / a | b/g, matching characters’ abcd ‘, get all the a and b

4. The qualifier

* : matches the previous subexpression zero or more times.

Example: /a*/g, matches the character ‘aaaaabc’, gets all aaaaa

{n} : n is a non-negative integer. Matches the specified n times.

Example: /a{1}/g, matches the character ‘aaaaabc’, and gets an A

{n,} : n is a non-negative integer. At least n matches.

Example: /a{1,}/g, matches the character ‘aaaaabc’, gets all aaaaa

{n,m} : both m and n are non-negative integers, where n <= m, matches at least n times and at most m times.

Example: /a{1,3}/g, matches the character ‘aaaaabc’, and gets up to three aaa’s

5. Locator

\b: Matches a word boundary, the position between a word and a space.

Example: /a\b/g, matches the character ‘a BC ‘, and gets a with space independent

\B: Non-word boundary matching.

Example: /a\B/g, matches the character ‘ab c’ to get a in the sequence

Commonly used 15 JS regular expressions

Var STR = /^[a-za-z0-9_ -]{4,16}$/; // The password must contain at least six characters, including at least one uppercase letter, one lowercase letter, one digit, and one special character. Var STR = /^. (=. {6})? =.*d)(? =.*[A-Z])(? =.*[a-z])(? =. * [! @ # $% ^ & *?] ). * $/; Var STR = /^d+$/; Var STR = /^-d+$/; Var STR = /^-? d+$/; Var STR = /^d*.? d+$/; Var STR = /^-d*.? d+$/; Var STR = /^-? d*.? d+$/; Regular var STR = / / Email / ^ ([- Za - z0-9 _ -. "]) + @ ([A - Za - z0-9 _ -.]) +. ([A Za - z] {2, 4}) $/; Var STR = /^1[34578]d{9}$/; / / id number (18) regular var STR = / ^ {5} [1-9] d (18 19 | | ([23] d)) d {2} ((0 [1-9]) | | (10, 11 | 12)) (([0-2] [1-9]) 10 20 | | | | 30 31) d {3} [xx] 0-9 $/; Var STR = /^((HTTPS? |ftp|file)://)? ([da - z -] +). ([a-z.] {2, 6}) (*) [/ w. -] * /? $/; Var STR = /^(? : (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) .). {3} (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) $/; Var STR = /^#? ([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/; / date/regular, simple decision, do not make month and date of var STR = {4} / ^ d (-) d {1, 2} {1, 2} $1 d /; Var STR = /^[1-9][0-9]{4,10}$/; / / WeChat ID regular, 6 to 20, begin with a letter, letters, Numbers and minus sign, underline the var STR = / ^ [a zA - Z] ([9] - _a - zA - Z0 - {5} 3) + $/; / / license number regular var STR = / ^ [beijing-tianjin Shanghai yu ji yu cloud liao black xiang anhui new GuiGan of hubei province can Sue your yue jin meng shan ji min qinghai-tibet plain NingQiong that brought A - Z] {1} {1} [a-z] [A - Z0-9] {4} [A - Z0-9 hang cop Hong Kong and Macao] {1} $/; Var STR = /[u4e00-u9fa5]/; var STR = /[u4e00-u9fa5]/;Copy the code

Finished? Then you are a handsome bee