This example regular expression (^[\u0021-\u003F\u0041-\uFFEF]+@[\u0021-\u003F\u0041-\uFFEF]+\.[\u0021-\u003F\u0041-\uFFEF]+$
) can filter characters by their Unicode character ranges, and I can make ranges go from \u0000
to \uFFFF
, but Unicode supports characters beyond 0xFFFF
. Can I use ranges above these in JavaScript's regular expressions?
此示例正则表达式(^ [\ u0021- \ u003F \ u0041- \ uFFEF] + @ [\ u0021- \ u003F \ u0041- \ uFFEF] + \。[\ u0021- \ u003F \ u0041- \ uFFEF] + $)可以按Unicode字符范围过滤字符,我可以使范围从\ u0000到\ uFFFF,但Unicode支持超过0xFFFF的字符。我可以在JavaScript的正则表达式中使用上面的范围吗?
2 个解决方案
#1
1
For backwards compatibility with ECMAScript 5 and older environments, the unfortunate solution is to use surrogate pairs:
为了向后兼容ECMAScript 5和旧环境,不幸的解决方案是使用代理对:
>> '\uD83D\uDCA9' '????' // U+1F4A9 PILE OF POO
In that case, each escape represents the code point of a surrogate half. Two surrogate halves form a single astral symbol.
在这种情况下,每个转义代表代理一半的代码点。两个代理半部形成一个单一的星体符号。
#2
1
ECMAScript6 introduces the RegExp
u
flag to support Unicode escapes of higher values:
ECMAScript6引入了RegExp u标志以支持更高值的Unicode转义:
(/\u{1F4A9}/u).test("????"); // true
Unfortunately, as of now only MS Edge supports this.
不幸的是,截至目前只有MS Edge支持此功能。
References
- MDN entry for
RegExp.prototype.unicode
RegExp.prototype.unicode
in the ES6 spec
RegExp.prototype.unicode的MDN条目
ES6规范中的RegExp.prototype.unicode
#1
1
For backwards compatibility with ECMAScript 5 and older environments, the unfortunate solution is to use surrogate pairs:
为了向后兼容ECMAScript 5和旧环境,不幸的解决方案是使用代理对:
>> '\uD83D\uDCA9' '????' // U+1F4A9 PILE OF POO
In that case, each escape represents the code point of a surrogate half. Two surrogate halves form a single astral symbol.
在这种情况下,每个转义代表代理一半的代码点。两个代理半部形成一个单一的星体符号。
#2
1
ECMAScript6 introduces the RegExp
u
flag to support Unicode escapes of higher values:
ECMAScript6引入了RegExp u标志以支持更高值的Unicode转义:
(/\u{1F4A9}/u).test("????"); // true
Unfortunately, as of now only MS Edge supports this.
不幸的是,截至目前只有MS Edge支持此功能。
References
- MDN entry for
RegExp.prototype.unicode
RegExp.prototype.unicode
in the ES6 spec
RegExp.prototype.unicode的MDN条目
ES6规范中的RegExp.prototype.unicode