Create a regular expression object

Constructor to create (RegExp)

Special symbols need to be escaped
let reg = new RegExp("Regular expression")
// Special symbols do not need to be escaped
let reg = new RegExp(/ Regular expression /)
Copy the code

Create a literal (//)

let reg = / Regular expression /

Copy the code

Correlation function

Test (Check whether a match is successful)

// Check whether the string matches, return a Boolean value/ regular expression /.test(string)Copy the code

Match (match one or all)

Return an array

// Match a single, return the first matchedString. The match (/ Regular expression /)
// add g to indicate all matches
// String matchAll(/ regular expression /)String. The match (/ regular expression /g)
// I: ignores case and returns the first matchedString. The match (/ regular expression/I)
// gi: ignore case and match allString. The match (/ regular expression /gi)
Copy the code

matchAll

Return an array

String. MatchAll (/ Regular expression /)
Copy the code

Exec (matches single or all)

Return an array

let str = China Mobile :10086, China Unicom :10010, China Telecom :10000
let reg = /\d{5}/g
// Output one at a time
console.log(reg.exec(string))// 10086(数组的形式)
console.log(reg.exec(string))// 10010(数组的形式)
console.log(reg.exec(string))// 10000(数组的形式)
console.log(reg.exec(string))// null
Copy the code

The replace (replace)

Returns the replaced string

// Replace only the first matchString. The replace (/ Regular expression /, the content of the replacement)// Replace all matchesString. The replace (/ regular expression /g, the content of the replacement)// m: indicates each matchString. The replace (/ regular expression /g.m= > m.toLowerCase())
Copy the code

Regular matching rule

Matching a single character

  1. . : Matches any character except \n(newline)
  2. [] : matches the characters listed in []
    ret = re.match("The Fast and the Furious [126789]"."The Fate of the Furious 2")
    ret = re.match("The Fast and the Furious [1-35-9]"."Fast and Furious 4"[.]: represents a point.([]: can be used as an escape character)Copy the code
  3. [^] : reverses the characters listed inside
    ret=re.match("Fast and Furious [^ 1A]"."Fast and Furious")
    Copy the code
  4. \d: Match the number, namely 0-9
  5. \D: Match non-numbers
  6. \ S: Matches blank space, including space, TAB key, enter (\r), newline (\n),\t
  7. \S: Match non-blank
  8. \ W: Matches word characters, i.e. letters, digits, and underscores (0-9, A-z, A-z,_)
  9. \W: Matches non-word characters

Matching multiple characters

  1. {m}: matches the previous character m times
  2. {m,n}: matches the previous character m to n times
  3. {m,}: matches the previous character to appear m times or more
  4. ? : The previous character appears 1 or 0 times
  5. *: matches the previous character for 0 or more times
  6. +: matches the previous character appears 1 or more times

Match beginning and end

  1. ^ : a string that must start with XXX (regular expression content)
  2. $: a string that must end with XXX (regular expression ending content)

Match the grouping

  1. | : logic or, matching around an arbitrary expression

  2. () : Grouping can also improve the priority

    // Groups 1,2, and 3 are all part of the regular expressionString. The match (/(Group 1)(Group 2)(Group 3)/g)
    console.log(RegExp. $2)// Outputs the content matched by group 2
    
    // Upgrade priority, the middle one first
    [0-9]|([a-z])|[A-Z]
    Copy the code
  3. \num: refers to the string matched by the group num

    Example: Check the validity of HTML page syntax (HTML double tags must be paired)

    // \1: references the string matched by group 1
    ret = '<h1>dsafagd</h1>'.match(/<(\w+)>.*</\ \1> /,Copy the code
  4. (? P

    ): group alias

  5. (? P=name): refers to the string matched by the group alias name

    Example: Check the validity of HTML page syntax (HTML double tags must be paired)

    // The first way
    ret = '<body><h1>dsafagd</h1></body>'.match(/<(\w+)><(\w+)>.*</\ \2><1 > / / \ \)
    // Second way: alias the group
    ret = '<body><h1>dsafagd</h1></body>'.match(/ 
            \w+)><(? P
            
             \w+)>.*</
            (? P=p2)></ (? P=p1)>/)
    Copy the code

Greed and non-greed

  1. Quantifiers in Python are greedy by default (or non-greedy by default in a few languages), matching as many characters as possible; Non-greedy, on the other hand, matches as few characters as possible. (The premise of greed and non-greed is to satisfy the overall matching results)
  2. In the “*”, “?” ,”+”,”{m,n}” after the quantifier? And turn greed into ungreed
    *? : 0A +? :1A???? :0A {m, n}? : mCopy the code

Common regular expressions

An expression for a checksum number

  1. Digital: r “^ \ d * $”
  2. N digits: r”^\d{n}$”
  3. At least n digits: r”^\d{n,}$”
  4. R “^\d{m,n}$”
  5. “The number of zero and non-zero start: r ^ (0 | [1-9] [0-9] *) $”
  6. Nonzero number with two decimal places: most of the beginning of r “^ (1-9] [\ | d * (1-9] \ d * \ d {1, 2}) $”
  7. Positive or negative numbers with 1-2 decimal places: r”^(-)? \ d + (. \ d {1, 2})? $”
  8. “Positive, negative, and decimal: r ^ (+) – |? \d+(.\d+)? $”
  9. R “^\d+(.[0-9]{2})$”
  10. R “^[0-9]+(.[0-9]{1,3})$”
  11. Non-zero positive integers: r ^ “\ [1-9] d *” or “r” “([1-9] [0-9] ∗) 1, 3” or “r” “^ (1-9 [] [0-9] *) {1, 3}” or “r” “([1-9] [0-9] ∗) 1, 3” or “r” “^ +? [1-9] [0-9] * $”
  12. Nonzero negative integer: r ^ “- [1-9] [0-9] *” or “r” “- [1-9] \ d ∗” or “r” “^ – [1-9] \ d *” or “r” “- [1-9] \ d ∗.” “
  13. Non-negative integers: r ^ \ d + “or” r “, “\ [1-9] d ∗ ∣ 0” or “r” “^ 1-9] [\ d * | 0” or “r”, “\ [1-9] d ∗ ∣ 0”
  14. “Not a positive integer: r ^ – [1-9] \ d * | 0” or “r” “((- \ d +) ∣ (0 +))” or “r” “^ ((\ d +) | (0 +))” or “r” “((- \ d +) ∣ (0 +))”
  15. Nonnegative floating point Numbers: r “^ \ d +” or “r” “^ 1-9] [\ d * \ \ d * \ | 0. [1-9] \ d \ d * * | 0? \. | 0 + 0”
  16. Non-positive float: r”^((-\d+(.\d+)?) | ((. 0 0 + +)?) ) or “r” ^ (- (1-9] [\ d * \ \ d * \ | 0. [1-9] \ d \ d * *)) | 0? \. | 0 + 0″
  17. Are floating point Numbers: r ^ “[1-9] \ d * \ | 0. D * (1-9] \ d \ d * *” or “r” “^ (([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \ [0-9] +) | ([0-9] * [1-9] [0-9] *))”
  18. Negative floating point: R “^ – (1-9] [\ d * \ d * | 0. 1-9] [\ \ d * d *)” or “r” “^ (- (([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \ [0-9] +) | ([0-9] * [1-9] [0-9] *)))”
  19. Floating point: r”^(-? \d+)(.\d+)?” Or r ^ -?” ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0? \. | 0 0 +)”

An expression to validate a character

  1. Chinese characters: r ^ “[\ u4e00 – \ u9fa5] {0} $”
  2. English and digital: r ^ “[9] A – Za – z0 – +” or “r”, “[A – Za – z0-9] 4 40” or “r” ^ “[A Za – z0-9] 40 {4}” or “r”, “[A – Za – z0-9] 4 40.” “
  3. All characters of length 3-20: r”^.{3,20}$”
  4. The value contains 26 letters: r”^[a-za-z]+$”
  5. A string of 26 uppercase letters: r”^[a-z]+$”
  6. A string consisting of 26 lowercase letters: r”^[a-z]+$”
  7. A string of 26 letters and digits: r”^[A-za-z0-9]+$”
  8. By the Numbers, 26 English letters or underscores string: r ^ \ w + “r” \ w3, 20 “or r” ^ \ w {3, 20} “or” r “\ w3, 20” “
  9. Chinese, English, Digits including underscore: r”^[\u4E00-\ u9FA5A-zA-z0-9_]+$”
  10. Chinese, English, Numbers, but not including the underline symbols such as: r ^ “[\ u4E00 – \ u9FA5A – Za – z0-9] +” or “r” ^ “[\ u4E00 – \ u9FA5A – Za – z0-9] {2, 20}”
  11. Can be entered with ^%&’,; =? \” and other characters: [^%&’,;=?\x22]+
  12. Forbid input containingThe character is [^] +

Special requirement expression

  1. Email Address: r”^\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*$”
  2. Domain name: r “[a zA – Z0-9] [9] – a – zA – Z0 – on conversion {0} (/. [a zA – Z0-9] [9] – a – zA – Z0 – on conversion {0}) + /.?”
  3. InternetURL: r “[a zA – z] + : / / [^ \ s] * or ^ http:// ([-] \ w +.) +[\w-]+(/[\w-./?%&=]*)? $”
  4. Mobile phone number: r ^ “([0-9] 13 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} $”
  5. Phone number (” XXX – XXXXXXX “, “XXXX – XXXXXXXX”, “XXX – XXXXXXX”, “XXX – XXXXXXXX”, “XXXXXXX” and “XXXXXXXX”) : r “^ ((\ d {3, 4} -) | \ d {} 3.4 -)? \ d {7, 8} $”
  6. Home phone number (0511-4405222, 021-87888822) : “\ d {3} – \ d {8} | \ d {4} – \ d {7}”
  7. Id number (15, 18 digits) : r “^ \ d {15} | \ d {and} $”
  8. Short id number (Numbers, the letter “x” at the end) : r “^ ([0-9] {7, 17} (x | x)? “Or” r “\” d8, 18 ∣ [0-9 x] 8, 18 ∣ [0-9 x] 8, 18?” Or r “^ \ d {8} 16 | x [0-9] {8} 16 | x [0-9] {8} 16?” Or r “\ d8, 18 ∣ [0-9 x] 8, 18 ∣ [0-9 x] 8, 18?”
  9. Account is (legal letter, allow 5-16 bytes, allow alphanumeric underlined) : r ^ “[a zA – Z] [a zA – Z0-9 _] {4, 15} $”
  10. Password (must start with a letter and contain only letters, digits, and underscores (_).): r”^[a-za-z]\w{5,17}$”
  11. Strong password (must contain uppercase and lowercase letters and digits, cannot use special characters, and is between 8 and 10 characters in length): r”^(? =.\d)(? =.[a-z])(? =. * [a-z]). 8, 10 {} $”
  12. Date format: r”^\d{4}-\d{1,2}-\d{1,2}”
  13. The twelve months of the year (01 ~ 09 and 1 ~ 12): r”^(0? [1-9] | 1 [2-0]) $”
  14. The 31 days of a month (01 ~ 09 and 1 ~ 31): [1-9]) | | 2 (1) ([0-9]) | | 30 31) $”
  15. Input format of money:
  16. 1. There are four kinds of representation of the money we can accept: “10000.00” and “10000.00,” and not “points” of the “10000” and “10000” : r ^ “[1-9] [0-9] * $”
  17. 2. This means that any one does not begin with 0, but it also means that a character “0” is not through, so we use the following format: r ^ “(0 | [1-9] [0-9] *) $”
  18. 3. A 0 or a number that does not start with 0. We can also allows the beginning with a minus sign: r ^ “(0 | -? [1-9] [0-9] *) $”
  19. 4. This represents a 0 or a potentially negative number that does not begin with a 0. Let the user start with 0. Let’s get rid of the minus, because money can’t be negative. R “^[0-9]+(.[0-9]+)? $”
  20. 5. Must be behind the decimal point should be at least 1 digit, so it is not through “10.”, but “10” and “10.2” is through: r “^ [0-9] + (. [0-9] {2})? $”
  21. 6. So we must have two decimal point behind, if you think that is too harsh, it can be: r “^ [0-9] + (. [0-9] {1, 2})? $”
  22. 7. This allows the user to write only one decimal digit. Below we should consider a comma in the digital, we can be like this: r “^ [0-9] {1, 3} ([0-9] {3}), * (. [0-9] {1, 2})? $”
    1. 1 to 3 Numbers, followed by any comma + 3 Numbers, commas become optional, rather than having to: r “^ ([0-9] + | [0-9] {1, 3} ([0-9] {3}), * (. [0-9] {1, 2})? $”
  23. Note: This is the final result, remember that “+” can be replaced with “*” if you think empty strings are acceptable (strange, why?). Finally, don’t forget to get rid of the backslash when you use a function, because that’s where the usual mistakes are
  24. R “^([a-za-z]+-?) +[a-zA-Z0-9]+\.[x|X][m|M][l|L]$”
  25. Regular expressions for Chinese characters: “[u4e00-u9fa5]”
  26. Double byte characters: “[^\x00-\ XFF]” (including Chinese characters, can be used to calculate the length of the string (a double byte character length 2, ASCII character 1))
  27. Regular expression for blank lines: “\n\s*\r” (can be used to delete blank lines)
  28. HTML tag regular expression: r”<(\S*?) [^ >]>.? < 1 > / \ | <. *? /> < span style = “max-width: 100%; clear: both; min-height: 1em;
  29. Fore and aft blank characters of regular expressions: r “^ | \ \ s * * ‘s or r” (\ s ∗) ∣ (\ s ∗ “or” r “” (^ \ s *) | (\ s *” or “r” “(\ s ∗) ∣ (\ s ∗)” (which can be used to delete rows first line end of white-space characters (including Spaces, tabs, form-feed character, etc.), very useful expressions)
  30. QQ id: r”[1-9][0-9]{4,}”
  31. Zip code: r”[1-9]\d{5}(? ! \d)” (6-digit Postal code in China)
  32. IP address: r”\d+.\d+.\d+.\d+”
  33. IP address: r”((? : (? :25[0-5]|2[0-4]\d|[01]? \d? \d)\.) {3} (? :25[0-5]|2[0-4]\d|[01]? \d? \d))