JavaScript ES(6-11)全版本语法 (十一):RegExp的y修饰符和u修饰符

352 阅读5分钟

前提概要

上一篇编写的是ES6中的字符串的扩展,链接:juejin.cn/post/701811… , 这次写的是ES6中RegExp的y修饰符和u修饰符,由于正则的内容涉及很多方面,这次仅仅介绍y修饰符和u修饰符,正则其他的知识点以后再慢慢补充。如有不对的或者不准确的地方,欢迎大家提出😄,我也积极修改。下边开始正文:

src=http___img.99danji.com_uploadfile_2021_0112_20210112041317153.jpg&refer=http___img.99danji.jpg

y修饰符

ES6为正则表达式添加了y修饰符,叫做“粘连”(sticky)修饰符。

y修饰符的作用与ES5中g修饰符类似,也是全局匹配,后一次匹配都从上一次匹配成功的下一个位置开始。不同之处在于,g修饰符只要剩余位置中存在匹配就可,而y修饰符确保匹配必须从剩余的第一个位置开始,这也就是“粘连”的涵义。

const str = 'aaa_aa_a'
const reg1 = /a+/g
const reg2 = /a+/y

console.log(reg1.exec(str)) // ['aaa', index: 0, input: 'aaa_aa_a', groups: undefined]
console.log(reg2.exec(str)) // ['aaa', index: 0, input: 'aaa_aa_a', groups: undefined]

console.log(reg1.exec(str)) // ['aa', index: 4, input: 'aaa_aa_a', groups: undefined]
console.log(reg2.exec(str)) // null

exec()方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null。

上面代码有两个正则表达式,一个使用g修饰符,另一个使用y修饰符。这两个正则表达式各执行了两次,第一次执行的时候,两者行为相同,剩余字符串都是_aa_a。由于g修饰没有位置要求,所以第二次执行会返回结果,而y修饰符要求匹配必须从剩余的第一个位置开始匹配,所以返回null。

如果改一下正则表达式,保证每次都能头部匹配,y修饰符就会返回结果了。

const str = 'aaa_aa_a'
const reg = /a+_/y

console.log(reg.exec(str)) // ['aaa_', index: 0, input: 'aaa_aa_a', groups: undefined]
console.log(reg.exec(str)) // ['aa_', index: 4, input: 'aaa_aa_a', groups: undefined]

上面代码每次匹配,都是从剩余字符串的头部开始。

使用lastIndex属性,可以更好地说明y修饰符。

const regexp = /a/g

// 指定从2号位置(y)开始匹配
regexp.lastIndex = 2

// 匹配成功
const match = regexp.exec('xaya')

// 在3号位置匹配成功
console.log(match.index) // 3

// 下一次匹配从4号位开始
console.log(regexp.lastIndex) // 4

// 4号位开始匹配失败
console.log(regexp.exec('xaxa')) // null

上面代码中,lastIndex属性指定每次搜索的开始位置,g修饰符从这个位置开始向后搜索,直到发现匹配为止。

y修饰符同样遵守lastIndex属性,但是要求必须在lastIndex指定的位置发现匹配。

const regexp = /a/y

// 指定从2号位置开始匹配
regexp.lastIndex = 2

// 不是粘连,匹配失败
console.log(regexp.exec('xaya')) // null

// 指定从3号位置开始匹配
regexp.lastIndex = 3

// 3号位置是粘连,匹配成功
const match = regexp.exec('xaxa')
console.log(match) // ['a', index: 3, input: 'xaxa', groups: undefined]
console.log(match.index) // 3
console.log(regexp.lastIndex) // 4

进一步说,y修饰符号隐含了头部匹配的标志^。

const reg = /b/y
console.log(reg.exec('aba')) // null
console.log(reg.lastIndex) // 0

sticky 模式在正则匹配过程中只会影响两件事:

  • 匹配必须从 reg.lastIndex 开始(相当于正则表达中的 ^)
  • 如果匹配到会修改 reg.lastIndex(相当于 g 模式)

u修饰符

ES6为正则表达式添加了u修饰符,含义为“Unicode模式”,用来正确处理大于 \uFFFF 的Unicode字符。也就是说,会正确处理四个字节的UTF-16编码。

console.log(/^\uD842/u.test('\uD842\uDFB7')) // false
console.log(/^\uD842/.test('\uD842\uDFB7')) // true

上面代码中, \uD842\uDFB7 是一个四个字节的UTF-16编码,代表一个字符'𠮷'。但是,ES5不支持四个字节的UTF-16编码,会将其识别为两个字符,导致第二行代码结果为true。加了u修饰符以后,ES6就会识别其为一个字符,所以第一行代码结果为false。

一旦加上u修饰符号,就会修改下面这些正则表达式的行为。

(1) 点字符

点(.)字符在正则表达式中,含义是除了换行符以外的任意单个字符。对于码点大于 0xFFFF 的 Unicode 字符,点字符不能识别,必须加上u修饰符。

let str = '𠮷'
console.log(/^.$/.test(str)) // false
console.log(/^.$/u.test(str)) // true

上面代码表示,如果不添加u修饰符,正则表达式就会认为字符串为两个字符,从而匹配失败。

(2) Unicode字符表示法

ES6新增了使用大括号表示Unicode字符,这种表示法在正则表达式中必须加上u修饰符,才能识别。

console.log(/\u{61}/.test('a')) // false
console.log(/\u{61}/u.test('a')) // true

上面代码表示,如果不加u修饰符,正则表达式无法识别\u{61}这种表示法,只会认为这匹配61个连续的u。

(3) 量词

使用u修饰符后,所有量词都会正确识别码点大于0xFFFF的Unicode字符。

console.log(/𠮷{2}/.test('𠮷𠮷')) // false
console.log(/𠮷{2}/u.test('𠮷𠮷')) // true

另外,只有在使用u修饰符的情况下,Unicode表达式当中的大括号才会被正确解读,否则会被解读为量词。

console.log(/^\u{3}$/.test('uuu')) // true

上面代码中,由于正则表达式没有u修饰符,所以大括号被解读为量词。加上u修饰符,就会被解读为Unicode表达式。

console.log(/\u{20BB7}{2}/u.test('𠮷𠮷')) // true

使用 u 修饰符之后 Unicode 表达式+量词也是可以的。

(4) 预定义模式

u修饰符也影响到预定义模式,能否正确识别码点大于0xFFFF的Unicode字符。

console.log(/^\S$/.test('𠮷')) // false
console.log(/^\S$/u.test('𠮷')) // true

上面代码的\S是预定义模式,匹配所有不是空格的字符。只有加了u修饰符,它才能正确匹配码点大于0xFFFF的Unicode字符。

利用这一点,可以写出一个正确返回字符串长度的函数。match() 方法检索返回一个字符串匹配正则表达式的结果。

function codePointLength(text) {
  const result = text.match(/[\s\S]/gu)
  return result ? result.length : 0
}
const s = '𠮷𠮷'
console.log(s.length) // 4
console.log(codePointLength(s)) // 2

(5) i修饰符

有些Unicode字符的编码不同,但是字型很相近,比如,\u004B与\u212A都是大写的K。

console.log(/[a-z]/i.test('\u212A')) // false
console.log(/[a-z]/iu.test('\u212A')) // true

上面代码中,不加u修饰符,就无法识别非规范的K字符。