Check the mailbox Regular Expressions
// check mailbox rule var The checkEmail = (the rules, value, the callback) => { const regEmail + @ + ([A-zA-Z0-9_-] = / ^ ([A-zA-Z0-9 _-]) ) + ([\. [a-zA-Z0-9 _-]]) + / IF (regEmail.text (value)) { return the callback () } the callback ( new new error ( "Please enter a valid mailbox ' )) }
Check the phone number regular expression
// check rules phone var checkMobile = (the rules, value, CB) => { const regMobile = / ^ (0 | 86 | 17951) (13 is [0-9] |? 15 [012 356 789] |. 17 [678] | 18 is [0-9] | 14 [57 is] |. 19 [0267]) [0-9]. 8} {$ / IF (regMobile.text (value)) { return CB () } CB ( new new error ( 'enter legitimate phone number ' )) }
Check fixed phone number regular expression
// check the fixed telephone number var checkTel = (the rules, value, CB) => { const regtel = / ^ (\ (\ D {3,4-} \) | \ D {3,4-} - | \ S) ? \ d {7, 14} $ / .test (tel) IF (regtel.text (value)) { return cb () } cb ( new new error ( 'Please enter a valid phone number' )) }
Check identity documents regular expressions
// check rule ID var checkIDcard = (the rules, value, CB) => { const regIDcard = (^ \ D {15} $) | (^ \. 17} {D ([0-9] | X-) $ ) IF (regIDcard.text (value)) { return CB () } CB ( new new error ( "Please enter a valid ID number ' )) }
Other common
Extracting information network link : (H | H) (R & lt | R & lt) (E | E) (F | F.) * * = ( '| ") (\ W | \\ | \ / | \.) +? ( '| " ? | * |>) to extract information in the e-mail address : \ w + ([- +.] \ w +) * @ \ w + ([-.] \ w +) * \ \ w + (. [-.] \ w +) * extracted information image links : (S | S) (R & lt | R & lt) (C | C) * = * ( '| ") (\ W | \\ | \ / | \) + (?. ? '| "| * |> ) to extract information in the IP address : (\ d +) \ (\ d +) \ (\ d +) \ (\ d +... ) extract information in Chinese telephone number (including mobile and fixed telephone) : (\ (\ D { ? 3,4-} \) | \ D {3,4-} - | \ S) \ D {7,14 } extract information in China ZIP : [ 1-9] . 1} {(\ + D). 5 { } extract information China ID number : \ D { 18 is} | \ D {15 } Extract information integers : \ d + extract information floating point (ie decimal) : ( - \ d *?) \ \ D +.? Extract information in any number : ( -? \ D *) (\ \. ? d +) to extract information in Chinese string : [\ u4e00 - \ u9fa5] * information extracted double-byte strings (kanji) : [ ^ \ x00- \ xFF] *
method | description |
---|---|
exec |
Performing a lookup in a method RegExp matching string, it returns an array (not match returns to null). |
test |
Test Method RegExp a match in the string, which returns true or false. |
match |
Find a matching String execution method in the string, it returns an array, when not to match returns null. |
matchAll |
Find all String methods to perform a match in the string, it returns an iterator (iterator). |
search |
Test method for matching a String in the string, it returns to the location index matched, or -1 on failure. |
replace |
Performing a lookup in a string matching method String, and used to replace the replace the substring matching string. |
split |
Using a regular expression or a character string separated by a fixed string, array and stores the substring to the partition String method. |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Special characters
Mark | description |
---|---|
g |
Global search. |
i |
Not case-sensitive search. |
m |
Multi-line search. |
s |
Allow . to match newline. |
u |
Use unicode mode code match. |
y |
The implementation of "sticky" search, matching the current position of the target from the beginning of the string, you can use the y flag. |
character | meaning |
\ | As I turn, which is usually in the "\" character is not back by the original interpretation of the meaning, such as / b / matches the character "b", when in front of b plus a backslash after the / \ b /, I turn to match a word border. |
^ | Matches the beginning of a line or input, / ^ a / match "an A", without matching "An a" |
$ | Or the end of a match input line, / a $ / matching "An a", instead of matching "an A" |
* | Match front metacharacters zero or more times, / ba * / will match b, ba, baa, baaa |
+ | Matches the previous meta character one or more times, / ba * / will match ba, baa, baaa |
? | Match front metacharacters 0 or 1, / ba * / will match b, ba |
(x) | Match x save x in a variable named $ 1 ... $ 9 |
x | and | Match x or y |
{n} | Exact match n times |
{n,} | N times more than match |
{n,m} | Matching nm times |
[xyz] | The character set (character set), matches any of the one set of a character (or character-membered) |
[^xyz] | It does not match any of the characters in this set |
[\b] | Matching a backspace |
\b | Matches a word boundary |
\B | Matches a non-word boundary |
\cX | Here, X is a control character, / \ cM / Ctrl-M Match |
\d | Matches a word character, / \ d / = / [0-9] / |
\D | A non-character words match, / \ D / = / [^ 0-9] / |
\n | Matches a newline |
\r | A carriage return match |
\s | Matching a blank character, including \ n, \ r, \ f, \ t, \ v etc. |
\S | 匹配一个非空白字符,等于/[^\n\f\r\t\v]/ |
\t | 匹配一个制表符 |
\v | 匹配一个重直制表符 |
\w | 匹配一个可以组成单词的字符(alphanumeric,这是我的意译,含数字),包括下划线,如[\w]匹配"$5.98"中的5,等于[a-zA-Z0-9] |
\W | 匹配一个不可以组成单词的字符,如[\W]匹配"$5.98"中的$,等于[^a-zA-Z0-9]。 |
使用方式
用re = new RegExp("pattern",["flags"]) 的方式比较好 |
vaScript动态正则表达式问题 请问正则表达式可以动态生成吗? |