When using a regex literal in JavaScript it's /-delimited, e.g. /^[a-z]$/.
When including a slash in that regex, i.e. /^[a-z/]$/, everything seems to work fine - both my IDE's syntax highlighter and the JS parsers of Firefox and Chrome accept it.
Is it standardized behavior that / does not need to be escaped within a character class or just a developer-friendly implementation and thus not work in all browsers?
Yes, this is defined by the specification (p. 25):
RegularExpressionClass
::
[RegularExpressionClassChars]RegularExpressionClassChars
::
[empty]
RegularExpressionClassChars RegularExpressionClassCharRegularExpressionClassChar
::
RegularExpressionNonTerminator but not one of]or\RegularExpressionBackslashSequenceRegularExpressionNonTerminator
::
SourceCharacter but not LineTerminator
In contrast, the normal RegularExpressionChar is defined as:
RegularExpressionChar
::
RegularExpressionNonTerminator but not one of\or/or[
RegularExpressionBackslashSequence
RegularExpressionClass
So within character classes you can use / freely without the need for escaping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With