正则的拓展
- RegExp 构造函数
//构造函数创建,参数是字符串,这时第二个参数表示正则表达式的修饰符
let str = new RegExp("[a-z]","i")
let a = "aa"
let b = 5
console.log(str.test(a)) //true
console.log(str.test(b)) //false
//字面量创建,参数是一个正则表示式,这时会返回一个原有正则表达式的拷贝
let str = /[a-z]+/i
let a = "aa"
let b = 5
console.log(str.test(a)) //true
console.log(str.test(b)) //false
- 字符串的正则方法
字符串对象共有 4 个方法,可以使用正则表达式:match()、replace()、search()和split(),ES6 将这 4 个方法,在语言内部全部调用RegExp的实例方法,从而做到所有与正则相关的方法,全都定义在RegExp对象上。
//String.prototype.match 调用 RegExp.prototype[Symbol.match]
//String.prototype.replace 调用 RegExp.prototype[Symbol.replace]
//String.prototype.search 调用 RegExp.prototype[Symbol.search]
//String.prototype.split 调用 RegExp.prototype[Symbol.split]
//match() 返回一个数组,可以再字符串内检测指定的值
let str = "hello hello"
let res = /llo/i
let res1 = /do/i
console.log(str.match(res)) //["llo", index: 2, input: "hello hello", groups: undefined]
console.log(str.match(res1)) //null
//replace() 字符串替换 g全局 i忽略大小写
let str = "hello hello"
let res = /llo+/g
let res1 = /do/i
console.log(str.replace(res,"*")) //he* he*
console.log(str.replace(res1,"*")) //hello hello
//search() 返回下标,找不到返回-1
let str = "hello hello"
let res = /llo/i
let res1 = /do/i
console.log(str.search(res)) //2
console.log(str.search(res1)) //-1
//trim()去除首尾空格
let str = " hello hello "
console.log(str.trim()) //hello hello
console.log(str) // hello hello
- u 修饰符
ES6 对正则表达式添加了u修饰符,含义为“Unicode 模式”,用来正确处理大于\uFFFF的 Unicode 字符。也就是说,会正确处理四个字节的 UTF-16 编码。一旦加上u修饰符号,就会修改下面这些正则表达式的行为。
//这里需要注意需要把我们的编码格式设置为UTF-16编码
//点字符,在正则表达式中,含义是除了换行符以外的任意单个字符。对于码点大于0xFFFF的 Unicode 字符,点字符不能识别,必须加上u修饰符。
var s = '𠮷';
console.log(/^.$/.test(s))// false
console.log(/^.$/u.test(s)) // true
//Unicode 字符表示法,ES6 新增了使用大括号表示 Unicode 字符,这种表示法在正则表达式中必须加上u修饰符,才能识别当中的大括号,否则会被解读为量词。
console.log(/\u{61}/.test('a'))// false
console.log(/\u{61}/u.test('a')) // true
console.log(/\u{20BB7}/u.test('𠮷')) // true
//量词,使用u修饰符后,所有量词都会正确识别码点大于0xFFFF的 Unicode 字符。
console.log(/𠮷{2}/.test('𠮷𠮷'))// false
console.log(/a{2}/u.test('aa')) // true
//预定义模式,\S是预定义模式,匹配所有非空白字符。只有加了u修饰符,它才能正确匹配码点大于0xFFFF的 Unicode 字符
console.log(/^\S$/.test('𠮷'))// false
console.log(/^\S$/u.test('𠮷')) // true
//i 修饰符
console.log(/[a-z]/i.test('\u212A'))// false
console.log(/[a-z]/iu.test('\u212A')) // true
//转义,没有u修饰符的情况下,正则中没有定义的转义(如逗号的转义\,)无效,而在u模式会报错。
console.log(/\,/) // /\,/
console.log(/\,/u) //Uncaught SyntaxError: Invalid regular expression: /\,/: Invalid escape
- RegExp.prototype.unicode 属性 ,正则实例对象新增unicode属性,表示是否设置了u修饰符。
let r1 = /hello/;
let r2 = /hello/u;
console.log(r1.unicode) //false
console.log(r2.unicode) //true
- y 修饰符,y修饰符的作用与g修饰符类似,也是全局匹配,后一次匹配都从上一次匹配成功的下一个位置开始。不同之处在于,g修饰符只要剩余位置中存在匹配就可,而y修饰符确保匹配必须从剩余的第一个位置开始,这也就是“粘连”的涵义。
var s = 'aaa_aa_a';
var r1 = /a+/g;
var r2 = /a+/y;
console.log(r1.exec(s)) //["aaa", index: 0, input: "aaa_aa_a", groups: undefined]
console.log(r2.exec(s)) //["aaa", index: 0, input: "aaa_aa_a", groups: undefined]
console.log(r1.exec(s)) //["aa", index: 4, input: "aaa_aa_a", groups: undefined]
console.log(r2.exec(s)) //null
- RegExp.prototype.sticky 属性,与y修饰符相匹配,ES6 的正则实例对象多了sticky属性,表示是否设置了y修饰符。
var r = /hello\d/y;
console.log(r.sticky) // true
- RegExp.prototype.flags,会返回正则表达式的修饰符。
console.log(/abc/ig.flags) //gi