Regular
create
data verification
Method 1: var reg = new RegExp ( 'web', 'g') plus 'g' global match
Method 2: var reg = / web / unquoted / / additive called a delimiter li [Condition] First matches
Object .test (str) returns a boolean
objects .exec (str) Returns an array (attached with the same twice returned) failed to resolve global search returns nullAa instanceof Array determines whether the object is an array
atom
\d 匹配0-9任意一个数字[0-9]
\D与除了数字以外的任意一个字符匹配[^0-9]
\w数字 字母 下划线匹配[a-zA-Z0-9]
\W除了数字 字母 下划线以外的任意一个字符[^a-zA-Z0-9]
\s与任意一个空白字符匹配
\S与任意一个空白字符意外的字符匹配[^\n\f\r\t\v]
\f 换页字符;
\n 换行字符;
\r 回车字符;
\t 制表符;
\v 垂直制表符;
Atom table
[ ] 只匹配其中的一个原子
[^] 只匹配"除了"其中字符的任意一个原子
[0-9] 匹配0-9任何一个数字
[a-z] 匹配小写a-z任何一个字母
[A-Z] 匹配大写A-Z任何一个字母
quantifier
* 重复零次或更多次
+ 重复一次或更多次
? 重复零次或一次
{n} 重复n次
{n,} 重复n次或更多次
{n,m} 重复n到m次
一片两片三四片,落尽正则全找见
Metacharacters
- Special characters that have special meaning called metacharacters
- Any character except a newline
- | Or mean, matching one on behalf of the match
- Examples: matching identification number, the old version is 15 digits, 18 digits of the new
- / ^ \ d {15} | \ d {18} $ /
will match membered /./ character \ turn intended effect
Greedy and stingy
Regular match is greedy, greedy prohibit use?
- *? Repeated any number of times, but less duplication wherever possible
- +? Repeated one or more times, but less duplication wherever possible
- ?? repeat 0 or 1, but less duplication wherever possible
- {N, m}? Repeated n to m times, but less repeated as
- {N,}? Repeated n times or more, but less duplication wherever possible
Matching border
- ^ Matches the beginning of the string
- $ Matches the end of the string, ignoring newline
Word boundary limits
- \ B matches a word boundary
- \ B matches a portion other than word boundaries