I’ve learned regular expressions many times, but after a while I forget them and don’t remember them when I need to use them. Today found time to do a regular arrangement, convenient for later review and reference.
Basics:
^ Up pointed
When the ^ is placed inside [], it means the inverse; Place outside [] to represent the beginning of a string on a line
Eg: /^[^a-b]w+/ matches a string that does not begin with a or b
The dot.
In square brackets: matches any character (except \n)
Outside square brackets: Matches any character. If you want to represent the dot itself, you escape it.
\d matches a number, equivalent to [0-9]
\D matches a non-number equivalent to [^0-9]
\s matches any whitespace character, including Spaces, tabs, feed characters, and so on. Equivalent to [\ \ n \ r \ t f \ v]
\w matches numbers or letters or underscores, equivalent to [a-za-z0-9_]
\W matches non-numbers and letters and underscores, equivalent to [^ a-za-z0-9_]
The asterisk * (quantity qualifier) matches 0 or infinite occurrences of the preceding subexpression, equivalent to {0,}.
The plus sign + (quantity qualifier) matches the previous subexpression occurring once or more, which is equivalent to {1,}
? Matches the preceding subexpression 0 or 1 times, which is equivalent to {0,1}
{m} matches the preceding subexpression m times
{m,} (quantity qualifier) matches the preceding subexpression at least m times
{m,n} matches the previous subexpression that occurs at least m times and at most n times
$matches the end of the string
B Matches the beginning or end of the word
B matches the non-beginning and end of the word
The square brackets [] [] indicate “or” logic eg: [ABC] indicate either a or b or c
Parentheses () used alone: grouping There are brackets | : said or logic
Forward and reverse assertions
(? =pattern) (? ! pattern) (? <=pattern) (? <! pattern)
Short lines – in square brackets
When – in the first character, denotes itself; When not in the first character, represents the interval [a-p] from A to P
Greed match
GREED MATCHES:
When you include quantitative modifiers in a regular expression, try to match as many characters as possible. /a.*b/, which will match the longest string starting with a and ending with b. Like the string aabab, it’s going to match the whole string. Converts greedy to lazy matches: Quantity qualifiers followed by question marks
assertions
Positive Affirmation :(? =pattern)
Positive negation :(? ! pattern)
Reverse Affirmation :(? <=pattern)
Reverse negation :(?
Assertions can be used superimposed
Examples of login passwords:
Contains size letters and numbers, at least 8 bits
let reg = /^(? = (. *? [a-z])(? = (. *? [A-Z])(? = (. *? [d])[a-zA-Zd]{8,}/g
The first three parentheses of this regular expression are three positive assertions, that is, the string must satisfy, starting at the beginning, satisfy /.*? [a-z] /, /. *? [a-z] / and /. *? / [d]. That’s both. There are capital letters, lowercase letters, numbers.
Examples of regex:
1, The expression of the decimal system:
Ideas: Find the number that precedes three numbers and place a comma after it
str.replace(/(d)(? =(d{3})+$)/g, function ($1) { console.log($1) return $1 + ',' })
2, mobile phone number in the middle of the mask
Idea: The item that needs to match is followed by four numbers
str.replace(/(d{3})(? =d{4}$)/, '***')
3. Remove the space before and after
Method one:
The beginning and end characters of a string
Tip: If you add the G modifier, only the Spaces starting with STR will be removed
str.replace(/(^s*)|(s*$)/g, '')
Method 2:
The item you want to match is preceded by a space
Tip: subitem (.*?) Can not remove the question mark in, after the removal of the end of the space will not be removed
str.replace(/^[s]*(.*?) [s]*$/, '$1')
Examples of non-fetching matches and regular expressions will be added later.