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

hippietrail
  • 15,848
  • 18
  • 99
  • 158
overexchange
  • 15,768
  • 30
  • 152
  • 347

1 Answers1

4

Yes, according to the spec this is now a valid escape sequence, however in order to enable support you must include the new u flag in the Regex definition:

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

Babel REPL

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • `SyntaxError: invalid regular expression flag u` on *firefox 45.0a2* and *chrome 47.0* – overexchange Dec 21 '15 at 08:40
  • @overexchange You can check the status of browser implementations [here](https://kangax.github.io/compat-table/es6/#test-RegExp_y_and_u_flags). Babel transpiles regular expressions so is probably the only feasible option right now – CodingIntrigue Dec 21 '15 at 08:41
  • Note that transpiled regular expressions may not work correctly when you deal with lone surrogates. – nhahtdh Dec 21 '15 at 08:52
  • Note that the `\u{XXX}` syntax seems to be unsupported in IE11 (fine in Edge though) – Dmitry Pashkevich Sep 14 '18 at 20:16