I can't understand when I need one and two backward slashes \
.
First, see an example:
const rxHttps = new RegExp("\/");
console.log(rxHttps.test("/")); // true
const rxQuestion = new RegExp("\\?");
console.log(rxQuestion.test("?")); // true
const rxAnotherQuestion = new RegExp("\?"); // Uncaught SyntaxError: Invalid regular expression: /?/: Nothing to repeat
In the above example, to use a character /
, it needs just one \
.
However, ?
needs two \
, or SyntaxError occurs.
This is really confusing. I can't make heads or tails of it.
Why are they different? Am I misunderstanding?