4

Can this regex literal syntax having Unicode escape sequence syntax,

var regpat= /^[\u0041-\u005A\u0061-\u007A\.\' \-]{2,15}/;

be written using Unicode code point escape syntax(as shown below)?

var regpat= /^[\u{41}-\u{5A}\u{61}-\u{7A}\u{1F4A9}\.\' \-]{2,15}/;

Note: Unicode code point escapes is used to simplify ES5-compatible surrogate pair syntax representing code point value more than FFFF

4

1 回答 1

4

是的,根据规范,这现在是一个有效的转义序列,但是为了启用支持,您必须在正则表达式定义中包含新的u标志:

var regpat = /^[\u{41}-\u{5A}\u{61}-\u{7A}\u{1F4A9}\.\' \-]{2,15}/u;
console.log(regpat.test("\u{41}\u{61}}"))

通天塔 REPL

于 2015-12-21T08:33:33.313 回答