Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't I have to escape 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.org/ecma-262/7.0/index.html but didn't come up with anything.

Can anyone help me find if the specification talks about escaping the equal sign?

like image 507
Gqqnbig Avatar asked Oct 18 '22 21:10

Gqqnbig


1 Answers

I'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 combination 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 combined with square brackets. The same applies to = with/without ?.

like image 163
crusy Avatar answered Nov 04 '22 18:11

crusy