ES6中y修饰符合u修饰符

y修饰符:也叫粘连(sticky)修饰符,作用与g修饰符类似,也是全局匹配

g修饰符只要剩余位置中存在匹配就可,而y修饰符确保匹配必须从剩余的第一个位置开始

 

对比:

// g
let a = '111  11 1'

let r1 = /\d+/g
console.log(r1.exec(a))
console.log(r1.exec(a))

Array [ "111" ]
Array [ "11" ]
// y
let a = '111  11 1'

let r2 = /^\d+/y
console.log(r2.exec(a))
console.log(r2.exec(a))

Array [ "111" ]
null

 

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

查看unicode的各种编码 https://www.fileformat.info/info/unicode/char/20bb7/index.htm

posted @ 2020-05-03 18:30  AllenZhang_(*^▽^*)  阅读(461)  评论(0编辑  收藏  举报