正则

字符类--预定义类

.   [^\n\r] 除了换行和回车之外的任意字符

\d [0-9] 数字字符

\D [^0-9] 非数字字符

\s [ \t\n\x0B\f\r] 空白字符

\S [^ \t\n\x0B\f\r] 非空白字符

\w [a-zA-Z_0-9] 单词字符

\W [^a-zA-Z_0-9] 非单词字符

特殊字符

\t /\t/ 制表符

\n /\n/ 换行符

\r /\r/ 回车符

\f /\f/ 换页符

\b /\b/ 与回退字符

\v /\v/ 垂直制表符

\0 /\0/ 空字符

字符类--负向类

括号内,前面加个元字符^进行取反,表示匹配不能为括号里面的字符。

console.log(/[^abc]/.test('a'));

console.log(/[^abc]/.test('ad'));

 

边界

^ 会匹配行或者字符串的起始位置

$ 会匹配行或字符串的结尾位置

^$在一起 表示必须是这个(精确匹配)

 

量词

"*"(贪婪) 重复零次或更多 (>=0)

"+"(懒惰) 重复一次或更多次 (>=1)

"?"(占有) 重复零次或一次 (0||1)

 

{n} n次 (x=n)

{n,} 重复n次或更多 (x>=n)

{n,m} 重复出现的次数比n多但比m少 (n<=x<=m)

* {0,}

+ {1,}

? {0,1}

简单类/ /

/abc/     字符串必须包含abc

方括号[]

[]    查找方括号之间的任何字符。

[^]  查找任何不在方括号之间的字符。

console.log("-------------负向类-------------");
console.log(/[^abc]/.test("iiii"));//true
console.log(/[^abc]/.test("aiiii"));//只有一部分睁一只眼闭一只眼true
console.log(/[^abc]/.test("abc"));//只有一部分睁一只眼闭一只眼true
console.log(/[^abc]/.test("b"));//只有一部分睁一只眼闭一只眼true
console.log(/[^abc]/.test("bcdef"));//只有一部分睁一只眼闭一只眼true
console.log(/[^abc]/.test("abcdef"));//只有一部分睁一只眼闭一只眼true
 
//边界
console.log(/^abc$/.test("abc"));
console.log(/^abc$/.test("a"));
console.log(/^abc$/.test("ac"));
console.log(/^abc$/.test("abcabc")); //false
 
console.log(/abc/.test("abcabc")); //true
 
 
posted @ 2018-05-13 18:21  V仔BOKE  阅读(100)  评论(0编辑  收藏  举报