why is regexp /\-/u a syntax-error?
Think of the u
flag as a strict mode for regular expressions.
/\a/u
throws, because there is no reason to escape a
as \a
--
therefore, if such an escape sequence is present, it's likely a user error.
The same goes for /\-/u
. -
only has special meaning within character
classes, not outside of them.
fyi, googling "tc39 regexp unicode" led to web-compat reasoning (learned something new) @ tc39/proposal-regexp-unicode-property-escapes#what-about-backwards-compatibility
What about backwards compatibility? In regular expressions without the u flag, the pattern \p is an (unnecessary) escape sequence for p. Patterns of the form \p{Letter} might already be present in existing regular expressions without the u flag, and therefore we cannot assign new meaning to such patterns without breaking backwards compatibility.
For this reason, ECMAScript 2015 made unnecessary escape sequences like \p and \P throw an exception when the u flag is set. This enables us to change the meaning of \p{…} and \P{…} in regular expressions with the u flag without breaking backwards compatibility.
jslint previously warned against unescaped literal "-" in regexp.
however, escaping "-" together with unicode flag "u", causes syntax error in chrome/firefox/edge (and jslint has since removed warning):
let rgx = /\-/u VM21:1 Uncaught SyntaxError: Invalid regular expression: /\-/: Invalid escape at <anonymous>:1:10
just, curious on reason why above edge-case is a syntax-error?