正则表达式
-
由一个字符序列形成的搜索模式
-
所有文本搜索和文本替换的操作
-
创建:
-
字面量(直接量)
// 在一对反斜线中写正则表达式内容,如/abc/ // 正则表达式里面不需要加引号 不管是数字型还是字符串型 var reg = /正则表达式/修饰符; var reg = /hello/g; var reg = /hello/img;//img是修饰符
-
构造函数
//构造正则表达式的实例,如new RexExp('abc') //内部传入的参数为字符串/字符串的变量 var reg =new RegExp("正则表达式","修饰符") var reg =new RegExp("hello","g");
-
-
字符
-
普通字符:除了特殊字符其他都是
-
特殊字符:
\:将特殊字符转义成普通字符
-
模式修饰字符
i:ignoreCase,忽略大小写
m:multiline,多行匹配
g:global,全局匹配
-
-
实例方法
-
exec
-
返回值数组: [匹配的内容,index: 在str中匹配的起始位置,input: 参数字符串,groups: undefined] , 没有匹配值返回null
var str = 'hello world hello'; var reg1 = /hello/; var reg2 = /hello/g; var reg3 = /exe/g; console.log(reg1.exec(str)); //[ 'hello', index: 0, input: 'hello world hello', groups: undefined ] console.log(reg2.exec(str)); //[ 'hello', index: 0, input: 'hello world hello', groups: undefined ] console.log(reg3.exec(str)); // null // 如果是全局模式的正则验证 还可以使用循环进行输出 var str = 'aa aas fdsfdaf dgfh'; var reg = /a/g while (true) { var result = reg.exec(str); if (!result) { break; } console.log(result[0], '--', result["index"], 'lastIndex:', reg.lastIndex); } // a-- 0 lastIndex: 1 // a-- 1 lastIndex: 2 // a-- 3 lastIndex: 4 // a-- 4 lastIndex: 5 // a-- 12 lastIndex: 13
-
有g的重要:
有"g",实例reg中会维护lastIndex属性,记录下一次开始的位置,当第二次执行exec的时候,从lastIndex开始检索
没有"g",不会维护lastIndex属性,每次执行从开始位置检索
var str = 'aaaasfdsfdgfdgfh'; //没有g时每次检验是从0开始,加g后会保存校验记录 var reg = /a/g console.log(reg.exec(str)); console.log(reg.exec(str)); console.log(reg.exec(str)); console.log(reg.lastIndex);//3 // [ 'a', index: 0, input: 'aaaasfdsfdgfdgfh', groups: undefined ] // [ 'a', index: 1, input: 'aaaasfdsfdgfdgfh', groups: undefined ] // [ 'a', index: 2, input: 'aaaasfdsfdgfdgfh', groups: undefined ]
-
-
test
-
返回true,false
var str = 'hello world'; var reg1 = /world/; var reg2 = /Regex/; console.log(reg1.test(str)); //返回true console.log(reg2.test(str)); //返回false
-
有无g的效果和exec一样
var str = 'aaaasfdsfdgfdgfh'; var reg = /a/g console.log(reg.test(str));//true console.log(reg.lastIndex);//1 console.log(reg.test(str));//true console.log(reg.lastIndex);//2
-
-
toString/toLocaleString
把正则表达式的内容转化成字面量形式字符串/有本地特色的字符串(JS中没效果)
var reg1 = /hello/; console.log(reg1.toString()); // /hello/ 字符串 console.log(reg1.toLocaleString()); // /hello/ 字符串
-
valueOf
返回正则表达式本身
var reg1 = /hello/img; console.log(reg1.valueOf()); // /hello/gim
-
-
实例属性
-
lastIndex
var str = 'hello hello hello'; var reg1 = /hello/; var reg2 = /hello/g; //没有g console.log(reg1.lastIndex); // 0 console.log(reg1.exec(str)); // 返回第一个hello console.log(reg1.lastIndex); // 0 //加了g console.log(reg2.lastIndex); // 0 console.log(reg2.exec(str)); // 返回第一个hello console.log(reg2.lastIndex); // 5 console.log(reg2.lastIndex); // 5 console.log(reg2.exec(str)); // 返回第二个hello console.log(reg2.lastIndex); // 11 console.log(reg2.lastIndex); // 11 console.log(reg2.exec(str)); // 返回第三个hello console.log(reg2.lastIndex); // 17 console.log(reg2.exec(str)); //返回 null console.log(reg2.lastIndex); // 0 console.log(reg2.exec(str)); // 返回第一个hello
-
没设置全局匹配g,属性值始终为0
-
设置了全局匹配g,每执行一次exec/test,lastIndex移向匹配到的字符串的下一个位置。
当指向的位置后没有可以再次匹配的字符串时,下一次执行exec返回null,test执行返回false,然后lastIndex归零,从字符串的开头重新匹配一轮
-
每次正则查找的起点就是lastIndex
-
-
ignoreCase、global、multiline、source
var reg1 = /hello/igm; console.log(reg1.ignoreCase); //true 检查是否有忽略大小写 console.log(reg1.global); //true 检查是否有全局匹配 console.log(reg1.multiline); //true 检查是否有多行匹配 //返回**字面量形式**的正则表达式(**类似于toString**) var reg1 = /hello/igm; console.log(reg1.source); //hello
-
-
语法-元字符
-
直接量字符
字母和数字字符 自身 \o Null字符 \t 制表符 \n 换行符 \v 垂直制表符 \f 换页符 \r 回车符 -
字符集合 []
-
匹配集合中的任意一个字符
-
英文连字符‘-’指定一个范围
-
如 [abc] 只要包含有a 或者 包含有b 或者包含有c 都返回为true
-
[0-9] 查找任何从0至9的数字……
var str = 'abc qwe abd1' var reg1 = /[0-9]/igm; console.log(reg1.test(str)); //true
-
[^xyz]一个反义或补充字符集(反义字符组),匹配任意不在方括号内的字符
var str = 'abc qwe abd1,2' var reg1 = /[^abc ]/igm; console.log(reg1.exec(str)); //[ 'q', index: 4, input: 'abc qwe abd1,2', groups: undefined ] var str = 'abc' var reg1 = /[^abc ]/igm; console.log(reg1.exec(str)); //null
-
-
边界符
-
^ 匹配输入开始,多行(multiline)标志被设为 true,该字符也会匹配一个断行(line break)符后的开始处。
-
$ 匹配输入结尾,多行(multiline)标志被设为 true,该字符也会匹配一个断行(line break)符的前的结尾处。
-
^和 $ 在一起,表示必须是精确匹配
// 必须是以abc开头的字符串才会满足 var reg = /^abc/; console.log(reg.test('abc')); // true console.log(reg.test('abcd')); // true console.log(reg.test('aabcd')); // false // 必须是以abc结尾的字符串才会满足 var reg = /abc$/; console.log(reg.test('abc')); // true console.log(reg.test('qweabc')); // true console.log(reg.test('aabcd')); // false // 精确匹配 要求必须是 abc字符串才符合规范 var reg1 = /^abc$/; console.log(reg1.test('abc')); // true console.log(reg1.test('abcd')); // false console.log(reg1.test('abcabc')); // false
-
-
字符集合"[]"与"^"和"$"一起使用
//26个英文字母任何一个字母返回 true - 表示的是a 到z 的范围 var reg = /^[a-z]$/ console.log(reg.test('a'));//true console.log(reg.test('z'));//true console.log(reg.test('A'));//false //字符组合 // 26个英文字母(大写和小写都可以)任何一个字母返回 true var reg1 = /^[a-zA-Z0-9]$/; //取反 方括号内部加上 ^ 表示取反,只要包含方括号内的字符,都返回 false 。 var reg2 = /^[^a-zA-Z0-9]$/; console.log(reg2.test('a'));//false console.log(reg2.test('B'));//false console.log(reg2.test(8));//false console.log(reg2.test('!'));//true
-
有无[]的区分
var str = 'hello wo hel reg hw;ddf' // ^以...开始 // 以h或w开头 var reg1 = /^[hw]/; console.log('以h或w开头',reg1.exec(str)); // 以h或w开头 [ 'h', index: 0, input: 'hello wo hel reg hw;ddf', groups: undefined ] // 以hw开头 var reg2 = /^hw/; console.log('以hw开头',reg2.exec(str)); // 以hw开头 null // 以df结尾 var reg3 = /df$/ console.log('df结尾',reg3.exec(str)); // df结尾 [ // 'df', // index: 21, // input: 'hello wo hel reg hw;ddf', // groups: undefined // ] // 以d或f结尾 var reg4 = /[df]$/ console.log('df结尾',reg4.exec(str)); // df结尾 [ 'f', index: 22, input: 'hello wo hel reg hw;ddf', groups: undefined ] // 以df结尾 var reg5 = /^df$/; console.log('df结尾',reg5.exec(str)); // df结尾 null // 精确查找 var reg6 = /^hello wo hel reg hw;ddf$/; console.log('精确查找hello wo hel reg hw;ddf',reg6.exec(str)); // 精确查找hello wo hel reg hw;ddf [ // 'hello wo hel reg hw;ddf', // index: 0, // input: 'hello wo hel reg hw;ddf', // groups: undefined // ] // 只有h/e/l任意一个 var reg7 = /^[hel]$/; console.log('精确查找',reg7.exec(str)); //精确查找 null console.log('精确查找',reg7.exec('h')); // 精确查找 [ 'h', index: 0, input: 'h', groups: undefined ]
-
\b 匹配一个零宽单词边界,表示一个单词(而非字符)边界
\B 匹配一个零宽非单词边界,与"\b"相反
var str = 'Hello World Hello JavaScript'; var reg1 = /\bHello\b/g; //完整单词 var reg2 = /\BScrip\B/g; //单词内部 console.log(reg1.exec(str)); // [ // 'Hello', // index: 0, // input: 'Hello World Hello JavaScript', // groups: undefined // ] console.log(reg2.exec(str)); // [ // 'Scrip', // index: 22, // input: 'Hello World Hello JavaScript', // groups: undefined // ]
-
字符类
字符类 含义 . 匹配除换行符\n和回车符之外的任何单个字符,等效于[^\n\r] \d 匹配一个数字字符,等效于[0-9] \D [^0-9] \w 匹配包括下划线的任何单个字符,包括A~Z,a~z,0~9和下划线"",等效于[a-zA-Z0-9] \W [^a-zA-Z0-9_] \s 匹配任何Unicode空白字符,包括空格、制表符、换页符等,等效于[\f\t\n\r] \S [^\f\t\n\r]
-
-
数量词
字符 含义 * >=0次 + ≥1 次 ? 0或1次 {n} n 次 {n,} ≥n 次 {n,m} n到m 次 // * 允许出现0次或多次 var reg = new RegExp(/^a*$/); console.log(reg.test("a")); // true console.log(reg.test("")); // true // + 允许出现1次或多次 var reg2 = new RegExp(/^a+$/); console.log(reg2.test("a")); // true console.log(reg2.test("")); // false // ? 只允许a出现1次或0次 var reg3 = new RegExp(/^a?$/); console.log(reg3.test("a")); // true console.log(reg3.test("")); // true console.log(reg3.test("aaa")); // false // {3} 允许重复3次 var reg4 = new RegExp(/^a{3}$/); console.log(reg4.test("a")); // false console.log(reg4.test("")); // false console.log(reg4.test("aaa")); // true console.log(reg4.test("aaaa")); // false // {3,} 允许重复出现3次或3次以上多次 var reg5 = new RegExp(/^a{3,}$/); console.log(reg5.test("a")); // false console.log(reg5.test("")); // false console.log(reg5.test("aaa")); // true console.log(reg5.test("aaaa")); // true // {3,6} 允许重复出现3次-6次之间,也就是>=3且<=6 var reg6 = new RegExp(/^a{3,6}$/); console.log(reg6.test("a")); // false console.log(reg6.test("aaaa")); // true console.log(reg6.test("aaaaaa")); // true console.log(reg6.test("aaaaaaa")); // false
-
重复方式
-
贪婪模式
-
尽可能多的匹配(首先取最多可匹配的数量为一组进行匹配),当匹配剩余的字符串,还会继续尝试新的匹配,直到匹配不到为止,为默认模式
-
// 对字符串"123456789",匹配其中的数字3-6次:\d{3,6},先匹配数字出现6次的字符串(123456),然后再从剩余字符串(789)中匹配出现数字3次的情况,剩余字符若没有出现数字3次则停止匹配.
var str = "123456789"; var reg = /\d{3,6}/g; console.log(reg.exec(str)); //[ '123456', index: 0, input: '12345678', groups: undefined ] console.log(reg.exec(str)); // [ '789', index: 6, input: '123456789', groups: undefined ] console.log(reg.exec(str)); // null var reg5=/\d[a-z]*/igm; var str='1ab2cdefg3' // *:大于等于0 console.log(reg5.exec(str)) //[ '1ab', index: 0, input: '1ab2cdefg3', groups: undefined ]
-
-
非贪婪模式
-
尽可能少的匹配(每次取最少匹配的数量为一组匹配),直到匹配不到为止
-
在量词后加?
-
// 对字符串"123456789",匹配其中的数字3-8次:\d{3,8},每轮匹配都只匹配3次,剩余字符没有出现数字3次则停止匹配.
var str = "12345678912"; var reg = /\d{3,8}?/g; console.log(reg.exec(str)); // [ '123', index: 0, input: '123456789', groups: undefined ] console.log(reg.exec(str)); // [ '456', index: 3, input: '123456789', groups: undefined ] console.log(reg.exec(str)); // [ '789', index: 6, input: '123456789', groups: undefined ] console.log(reg.exec(str));// null var reg5=/\d[a-z]*?/igm;//*:大于等于0,取等于0 var str='1ab2cdefg3' console.log(reg5.exec(str)) //[ '1', index: 0, input: '1ab2cdefg3', groups: undefined ]
-
-
-
选择、分组、引用
-
选择:|
-
匹配次序从左到右
-
var reg = /html|css|js/ console.log(reg.exec('qweqwehtmlcss')); // html var pattern = /hello2|1/img; var str = 'hello1abc2'; console.log(pattern.exec(str)) //[ '1', index: 5, input: 'hello1abc2', groups: undefined ]
-
-
分组:()
-
圆括号包裹的一个小整体成为分组
-
/(briup){3}/ 相当于 /briupbriupbriup/ var reg6 = /hello(2|1)/g; var str = 'hello1abc2hello2' console.log(reg6.exec(str)) console.log(reg6.exec(str)) // [ // 'hello1', // '1', // index: 0, // input: 'hello1abc2hello2', // groups: undefined // ] // [ // 'hello2', // '2', // index: 10, // input: 'hello1abc2hello2', // groups: undefined // ]
-
-
捕获$
-
被正则表达式匹配(捕获)到的字符串会被暂存起来。由分组捕获的字符串会从1开始编号
-
$1引用了第一个被捕获的串,$2是第二个,依次类推。从外到里
-
var reg = /((apple) is (a (fruit)))/ var str = "apple is a fruit" reg.test(str) // true RegExp.$1 // apple is a fruit RegExp.$2 // apple RegExp.$3 // a fruit RegExp.$4 // fruit
-
-
引用(反向引用)
-
var reg = /(\w{3}) is \1/ // /(\w{3}) is (\w{3})/ //\1捕获到第一个分组 //相当于首尾找3个字符,字符要相同 console.log(reg.test('kid is kid')); // true console.log(reg.test('dik is dik')); // true console.log(reg.test('kid is dik')); // false console.log(reg.test('dik is kid')); // false
-
编号越界,会被当成普通的表达式
var reg = /(\w{3}) is \6/; reg.test( 'kid is kid' ); // false reg.test( 'kid is \6' ); // true
-
-
-
String对正则表达式的支持
-
search
-
查找字符串中是否有匹配正则的字符串,有则返回字符串第一次出现时的位置,无则返回-1
-
不受全局g影响
var str = 'hello world hello'; var reg = /hello/; console.log(str.search(reg)); // 0 var reg=/\d{2,}/img; var str='hello1world'; console.log(str.search(reg)) //-1
-
-
match
-
匹配字符串中符合正则表达式的字符串,返回该字符串的一个数组,其中包括字符串内容、位置
-
如果正则设置全局匹配,则一次性返回所有符合正则表达式的字符串数组
-
添加分组,返回符合要求的字符串以及分组的一个数组
-
同时开启全局和分组,不会在数组中添加分组内容
var str = 'hello world hello'; // 匹配字符串,返回字符串数组:搜索的字符串、位置、全部字符串内容 var reg1 = /hel/; console.log(str.match(reg1)); //[ 'hel', index: 0, input: 'hello world hello', groups: undefined ] // 全局匹配,返回所有符合的字符串数组 var reg2 = /he/g; console.log(str.match(reg2)); //[ 'he', 'he' ] // 添加分组,返回整个正则字符串和分组字符串、位置、全部字符串内容 var reg3 = /(he)l/; console.log(str.match(reg3)); // [ // 'hel', // 'he', // index: 0, // input: 'hello world hello', // groups: undefined // ] // 同时开启全局和分组,不会在数组中添加分组内容 var reg4 = /(h)l/g; console.log(str.match(reg4)); //null var reg5 = /(he)l/g; console.log(str.match(reg5)); //[ 'hel', 'hel' ] var reg=/\w{2}/g; var str='hello1world'; console.log(str.match(reg)); //[ 'he', 'll', 'o1', 'wo', 'rl' ] //d是单独一个数,不满足条件被省略
-
-
split 分割
// 以某种形式分割字符串 split() var str = "terry134briup156lisi12zhangsan"; // 当数字出现一次或多次时 var reg = /\d+/; var result = str.split(reg); console.log(result); // [ 'terry', 'briup', 'lisi', 'zhangsan' ]
-
replace 替换
// 满足正则表达式条件的内容将被替换 var str = 'javascript' var reg = /javascript/; // replace(正则表达式, 要替换的内容),原内容不做改变 var result = str.replace(reg, 'java'); console.log(result); //java
-
-
前瞻表达式
表达式 名称 描述 (?=exp) 正向前瞻 匹配后面满足表达式exp的位置 (?!exp) 负向前瞻 匹配后面不满足表达式exp的位置 var str='a2*34v8'; var result=str.replace(/\w(?=\d)/g,'X'); //后面是\d的\w要做改变,a2,2*,*3,34,4v,v8,8'空字符' console.log(result); //X2*X4X8 var result=str.replace(/\w(?!\d)/g,'X'); //前面是\w的\d要做改变,a2,2*,*3,34,4v,v8,8'空字符' console.log(result) //aX*3XvX
-
字符汇总
手机号码验证:
-
以1为开头
-
第二位为3,4,5,7,8中的任意一位
-
最后以0-9的9个整数结尾
-
var reg = /^[1][34578]\d{9}$/ console.log(reg.test('13555555878')); //true console.log(reg.test('12555555878')); //false console.log(reg.test('135555558781')); //false console.log(reg.test('1355555587')); //false
密码验证:
包含:
-
大写
-
小写
-
数字
-
特殊字符(!,@,#,%,&)
-
大于6位
var reg=/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!|@|#|%|&])^([0-9a-zA-Z!@#%&]){6,}$/;