Favorite years of regular expressions Daquan notes, absolutely dry!

1, regular common
(1) Check the 6-digit postal code // The first one can not be 0
/^[1-9]\d{5}$/
(2) Check the compressed file //xxx.zip\xxx.gz\xxx.rar
/^\w+\.(zip|gz|rar)$/
(3) to remove extra spaces //
str.replace(/\s+/,'');
(4) delete both spaces
str.replace (/ ^ \ s + /, ''); // remove leading space
str.replace (/ \ s + $ /, ''); // trailing spaces removed
(5) // remove all spaces
str.replace(/\s/g,'');
(6) // remove the spaces before and after
str.replace((^\s+)|(\s+$),'')
(7) E-mail (xxxxx @ xxxx (.xxxx) +)
/^\w+@\w+(\.\w+)+$/
(8) phone number (any number beginning 1) 1 (3 | 5 | 7 | 8 | 4) \ d {9}
/^1\d{10}$/
(9) ID
/^\d{17}(\d|X)$/
422422 19660101 5810
421087 19890101 121X
^[1-9]\d{5}[19|20]\d{2}\d{7}(\d|X)$
(10) date (valid date format: xxxx-xxxx or xxxx / xx / xx or xxxx.xx.xx)
/^\d{4}[-\/\.]\d{2}[-\/\.]\d{2}$/
(11) can only enter Chinese
str.replace(/[^\u4e00-\u9fa5]/g,'');
(12) account name can only use alphanumeric underscore, and the numbers can not begin with a length of between 6-15
/ ^ [A-zA-Z _] \ w {} $ 5.14 /
(13) Verification IP
(xxx.)xxx.xxx.xxx|
254.245.255.255
240.196.19.5
/^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$/
(14) in a regular characters represented as [\ u4e00- \ u9fa5]
(15) expressed as numbers and letters A-Za-z0-9
(16) thus being part of the e-mail name expressions are summarized as follows: 
E-mail (xxxxx @ xxxx (.xxxx) +)
/^\w+@\w+(\.\w+)+$/
(17) phone number (any number beginning 1) 1 (3 | 5 | 7 | 8 | 4) \ d {9}
/^1\d{10}$/
 
2, regular expression special characters
\ W matches a character can form words (variable) (including letters, numbers, underscore)
The [\ W] Matching "$ 5.98" 5, is equivalent to [a-zA-Z0-9_].
\ W matches a character can not form words
The [\ W is] Matching "$ 5.98" $, is equal to [^ a-zA-Z0-9_].
\ N Matches a newline
\ R match a carriage return
\ T matches a tab
\ V matches a tab Shigenao
\ S matches a space character, including \ n, \ r, \ f, \ t, \ v etc.
\ S matches a non-blank character, equal / [^ \ n \ f \ r \ t \ v] /
\ B matches a word boundary (the word is a space separated)
/ \ Bm / matching "moon" the 'm';
/ Oon \ b / matches the "moon" the 'oon', since there is no word character string located at the end of 'oon', behind;
\ B Matches a non-word boundary
 
3, the amount of direct character (the escape character)
\ F formfeed
\ N newline
\ R carriage return
\ T Tab
\ V vertical tab
\ / A / direct amount
\\ a \ direct amount
\ A direct amount
\ * A * direct amount
\ + A + direct amount
\? A? Direct amount
\ | A | direct volume
\ (A (direct amount
\) A) the amount of direct
\ [A [direct amount
\] A] direct amount
\ {{A direct amount
\}} A direct amount
 
4, square brackets
[Abc] locate any character between the brackets.
[^ Abc] do not look for any character between the brackets.
[0-9] to find any number from 0 to 9. [1-9] \ d +
[Az] Find any character from lowercase a to lowercase z's.
[AZ] Find any character from uppercase A to Z of capital.
[Az] z Find any character from uppercase A to lowercase.
[Adgk] to find any character within a given collection.
[^ Adgk] to find any character outside the given set.
(Red | blue | green) to find any options specified.
 
5, metacharacters
Find a single character, except newline terminator and row, is equivalent to [^ \ n]
\ W may be composed of a matching-character words (variables) (including characters, numbers, underscore), such as [\ w] matches "$ 5.98" in the 5, is equivalent to [a-zA-Z0-9_].
\ W Find a non-word character. It may not match a word consisting of characters, such as [\ W is] matching "$ 5.98" $ is equal to [^ a-zA-Z0-9_].
\ D matches a numeric character, / \ d / equivalent / [0-9] /
\ D matches a non-numeric characters, / \ D / equivalent / [^ 0-9] /
\ S matches a space character, including \ n, \ r, \ f, \ t, \ v etc.
\ S matches a non-blank character, equal / [^ \ n \ f \ r \ t \ v] /
\ B matches a word boundary.
\ B matches non-word boundary.
\ 0 Find NUL characters.
\ N Find a newline.
\ F Find feed character.
\ R Find carriage return.
\ T Find tab.
\ V Find vertical tab.
\ Xxx xxx Find octal number specified character.
\ Xdd find a hexadecimal number dd illegal characters.
\ Uxxxx find Unicode character specified in hexadecimal number xxxx.
 
6, quantifiers
c {n} matching string comprising the sequence of n c.
c {m, n} m to match a string containing the sequence of the n c.
c {n,} matching string comprising a sequence of at least n c.
c + matches any string containing at least one of c, equivalent to c {1,}.
c * matches any string of zero or more c, equivalent to c {0,}
c? matches any string c is zero or one, is equivalent to c {0, 1}
c $ matches any string that ends c.
^ C matches any string beginning with c.
? = C match any of the immediately following specified string c string.
After them followed by "all" of "is" a global search.
var str="Is this all there is"; var patt1=/is(?= all)/g;
?! C did not match any subsequent immediately specified string c string.
After it is not followed by "all" of "is" a global search:
var str="Is this all there is"; var patt1=/is(?! all)/gi;

Guess you like

Origin www.cnblogs.com/lishixiang-007/p/11282720.html