For example:
"1+1=2".replace(/=/,"aa");
"1+1=2".replace(/\=/,"aa");
return the same result.
Does it mean I don't have to escape "=" (the equal sign) in JavaScript? I remembered that I always have to escape equal sign in Java and .NET.
I tried to find some info from .0/index.html but didn't e up with anything.
Can anyone help me find if the specification talks about escaping the equal sign?
For example:
"1+1=2".replace(/=/,"aa");
"1+1=2".replace(/\=/,"aa");
return the same result.
Does it mean I don't have to escape "=" (the equal sign) in JavaScript? I remembered that I always have to escape equal sign in Java and .NET.
I tried to find some info from https://www.ecma-international/ecma-262/7.0/index.html but didn't e up with anything.
Can anyone help me find if the specification talks about escaping the equal sign?
Share Improve this question edited Nov 24, 2016 at 0:37 asked Nov 24, 2016 at 0:31 user746461user746461 6-
12
=
has no special meaning in regular expressions, it doesn't need to be escaped. – Barmar Commented Nov 24, 2016 at 0:32 - 3 They don't escape it in Java in this question – Barmar Commented Nov 24, 2016 at 0:35
- 3 And here's a question that doesn't escape it: stackoverflow./questions/31490169/lookbehind-with-equal-sign – Barmar Commented Nov 24, 2016 at 0:36
-
@Barmar how would you explain the usage of = in Positive lookahead
(?= )
? I thought = is then a special character that always needs to be escaped.. – user746461 Commented Nov 24, 2016 at 0:50 -
3
Lots of characters have special meaning after
(?
, but they don't have special meaning elsewhere, so they don't need to be escaped. – Barmar Commented Nov 24, 2016 at 0:54
1 Answer
Reset to default 5I'd go with this table in MDN web docs. You'll see that =
has no special meaning - just as @Barmar stated. You referred to lookaheads, which use ?=
, but without the leading ?
it's just an equal sign.
There are not many special characters which needs an additional special character to "work", I guess that's why it's confusing. Think of it as -
in bination with [
and ]
:
0-9
matches "0-9"[0-9]
matches "0" and "1" and ... and "9"[0\-9]
matches "0" and "1" and ... and "9" and "-"
So just -
has no meaning and does not have to be escaped, only if bined with square brackets. The same applies to =
with/without ?
.