一.正则方法
(1).正则回顾
var reg = /\w/gim
['s', index:0,input:'sdjflsd']
(2).元字符转译
二.修饰符拓展
(1).修改内容
(2).声明正则的变更
var reg = new RegExp('xyz', 'ig') = var reg = /xyz/ig
var str = 'xyzxyz'
log(str.match(reg));
['xyz','xyz']
log(str.match(str));
['xyz','xyz']
var reg = new RegExp(/xyz/gi,'gm')
var str = 'xyz\nxyz';
log(str.match(reg));//多行依旧能匹配,以第二个参数为准 es6做的
['xyz','xyz']
(3).方法调整
RegExp.prototype[Symbol.match];
RegExp.prototype[Symbol.replace];
RegExp.prototype[Symbol.search];
RegExp.prototype[Symbol.split];
var reg = new RegExp('xyz','ig');
log(reg.global) true
log(reg.ignoreCase) true
log(reg.multiLine) false
var reg = /\wabed/giy
log(reg.source);//\wabed
log(reg.flags);//\giy
log('x##'.split(/#/y));//Array(3){0:'x' 1:'' 2:'' length:3}
log('x##'.split(/#/g));//Array(3){0:'x' 1:'' 2:'' length:3}
log('##x'.split(/#/y));//Array(3){0:'' 1:'' 2:'x' length:3}
log('#x#'.split(/#/y));//Array(3){0:'' 1:'x' 2:'' length:3}
console.log('x#x#xxxxx'.split(/#/y)); ["x", "x", "xxxxx"]
console.log('x#x#xxxx'.split(/#/g));["x", "x", "xxxx"]
/y/g在这里结果是一致得
var str = 'aaa_aa_a';
var reg1 = /a+/g;
var reg2 = /a+/y;
log(reg1.exec(str));//['aaa',index:0,input:'aaa_aa_a']
log(reg2.exec(str));//['aaa',index:0,input:'aaa_aa_a']
log(reg1.exec(str));//['aa',index:4,input:'aaa_aa_a']
log(reg2.exec(str));//null
y会判断第二个aa并不是第一个出现的而是中间出现了_这时候y就不能匹配到aa,第一个值必需要粘连到第二个值
.可以匹配任何字符包括\n等等特殊,es2018实现了,目前兼容可能有问题,所以用s
log(/foo.bar/.test('foo\nbar'))//false
log(/foo.bar/s.test('foo\nbar'))//true
log(/foo.bar/s.dotAll)//true
单纯是为了配合后面两个字符的编码的,单另拿出D842是乱码的
log('\uD842\uDFB7');//𠮷
log(/^\uD83D/.test('\uD83D\uDC2A'))//true
log(/^\uD83D/u.test('\uD83D\uDC2A'))//false
var s = '\uD842\uDFB7';
log(s);//𠮷
log(/^.$/.test(s));//false es5超出了编码极限无法匹配了
log(/^.$/u.test(s));//true
log(/\u{20bb7}/u.test('𠮷
'))
//true