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
- . : Matches any character except \n(newline)
- [] : 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
- [^] : reverses the characters listed inside
ret=re.match("Fast and Furious [^ 1A]"."Fast and Furious") Copy the code
- \d: Match the number, namely 0-9
- \D: Match non-numbers
- \ S: Matches blank space, including space, TAB key, enter (\r), newline (\n),\t
- \S: Match non-blank
- \ W: Matches word characters, i.e. letters, digits, and underscores (0-9, A-z, A-z,_)
- \W: Matches non-word characters
Matching multiple characters
- {m}: matches the previous character m times
- {m,n}: matches the previous character m to n times
- {m,}: matches the previous character to appear m times or more
- ? : The previous character appears 1 or 0 times
- *: matches the previous character for 0 or more times
- +: matches the previous character appears 1 or more times
Match beginning and end
- ^ : a string that must start with XXX (regular expression content)
- $: a string that must end with XXX (regular expression ending content)
Match the grouping
-
| : logic or, matching around an arbitrary expression
-
() : 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
-
\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
-
(? P
): group alias
-
(? 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
- 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)
- 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
- Digital: r “^ \ d * $”
- N digits: r”^\d{n}$”
- At least n digits: r”^\d{n,}$”
- R “^\d{m,n}$”
- “The number of zero and non-zero start: r ^ (0 | [1-9] [0-9] *) $”
- Nonzero number with two decimal places: most of the beginning of r “^ (1-9] [\ | d * (1-9] \ d * \ d {1, 2}) $”
- Positive or negative numbers with 1-2 decimal places: r”^(-)? \ d + (. \ d {1, 2})? $”
- “Positive, negative, and decimal: r ^ (+) – |? \d+(.\d+)? $”
- R “^\d+(.[0-9]{2})$”
- R “^[0-9]+(.[0-9]{1,3})$”
- 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] * $”
- Nonzero negative integer: r ^ “- [1-9] [0-9] *” or “r” “- [1-9] \ d ∗” or “r” “^ – [1-9] \ d *” or “r” “- [1-9] \ d ∗.” “
- Non-negative integers: r ^ \ d + “or” r “, “\ [1-9] d ∗ ∣ 0” or “r” “^ 1-9] [\ d * | 0” or “r”, “\ [1-9] d ∗ ∣ 0”
- “Not a positive integer: r ^ – [1-9] \ d * | 0” or “r” “((- \ d +) ∣ (0 +))” or “r” “^ ((\ d +) | (0 +))” or “r” “((- \ d +) ∣ (0 +))”
- Nonnegative floating point Numbers: r “^ \ d +” or “r” “^ 1-9] [\ d * \ \ d * \ | 0. [1-9] \ d \ d * * | 0? \. | 0 + 0”
- Non-positive float: r”^((-\d+(.\d+)?) | ((. 0 0 + +)?) ) or “r” ^ (- (1-9] [\ d * \ \ d * \ | 0. [1-9] \ d \ d * *)) | 0? \. | 0 + 0″
- 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] *))”
- 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] *)))”
- 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
- Chinese characters: r ^ “[\ u4e00 – \ u9fa5] {0} $”
- 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.” “
- All characters of length 3-20: r”^.{3,20}$”
- The value contains 26 letters: r”^[a-za-z]+$”
- A string of 26 uppercase letters: r”^[a-z]+$”
- A string consisting of 26 lowercase letters: r”^[a-z]+$”
- A string of 26 letters and digits: r”^[A-za-z0-9]+$”
- By the Numbers, 26 English letters or underscores string: r ^ \ w + “r” \ w3, 20 “or r” ^ \ w {3, 20} “or” r “\ w3, 20” “
- Chinese, English, Digits including underscore: r”^[\u4E00-\ u9FA5A-zA-z0-9_]+$”
- 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}”
- Can be entered with ^%&’,; =? \” and other characters: [^%&’,;=?\x22]+
- Forbid input containing
The character is [^] +
Special requirement expression
- Email Address: r”^\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*$”
- 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}) + /.?”
- InternetURL: r “[a zA – z] + : / / [^ \ s] * or ^ http:// ([-] \ w +.) +[\w-]+(/[\w-./?%&=]*)? $”
- 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} $”
- Phone number (” XXX – XXXXXXX “, “XXXX – XXXXXXXX”, “XXX – XXXXXXX”, “XXX – XXXXXXXX”, “XXXXXXX” and “XXXXXXXX”) : r “^ ((\ d {3, 4} -) | \ d {} 3.4 -)? \ d {7, 8} $”
- Home phone number (0511-4405222, 021-87888822) : “\ d {3} – \ d {8} | \ d {4} – \ d {7}”
- Id number (15, 18 digits) : r “^ \ d {15} | \ d {and} $”
- 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?”
- Account is (legal letter, allow 5-16 bytes, allow alphanumeric underlined) : r ^ “[a zA – Z] [a zA – Z0-9 _] {4, 15} $”
- Password (must start with a letter and contain only letters, digits, and underscores (_).): r”^[a-za-z]\w{5,17}$”
- 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 {} $”
- Date format: r”^\d{4}-\d{1,2}-\d{1,2}”
- The twelve months of the year (01 ~ 09 and 1 ~ 12): r”^(0? [1-9] | 1 [2-0]) $”
- The 31 days of a month (01 ~ 09 and 1 ~ 31): [1-9]) | | 2 (1) ([0-9]) | | 30 31) $”
- Input format of money:
- 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] * $”
- 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] *) $”
- 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] *) $”
- 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]+)? $”
- 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})? $”
- 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})? $”
- 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 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})? $”
- 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
- R “^([a-za-z]+-?) +[a-zA-Z0-9]+\.[x|X][m|M][l|L]$”
- Regular expressions for Chinese characters: “[u4e00-u9fa5]”
- 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))
- Regular expression for blank lines: “\n\s*\r” (can be used to delete blank lines)
- HTML tag regular expression: r”<(\S*?) [^ >]>.? < 1 > / \ | <. *? /> < span style = “max-width: 100%; clear: both; min-height: 1em;
- 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)
- QQ id: r”[1-9][0-9]{4,}”
- Zip code: r”[1-9]\d{5}(? ! \d)” (6-digit Postal code in China)
- IP address: r”\d+.\d+.\d+.\d+”
- IP address: r”((? : (? :25[0-5]|2[0-4]\d|[01]? \d? \d)\.) {3} (? :25[0-5]|2[0-4]\d|[01]? \d? \d))