Skip to content
Advertisement

how do I create a string with an escaped open parens?

consider the following

➜ ~ node
Welcome to Node.js v16.15.1.
Type ".help" for more information.
> s = '('
'('
> s = '('
'('
> s = '\('
'\('
> s = String.raw`(`
'('
> s = String.raw`(`
'\('
> s = String.raw`\(`
'\\('

So, how do I set s to '('?

Advertisement

Answer

You don’t.

You’re confused about the value of a string and its representation (whence Python repr() function).

A parenthesis is not a special character in a JavaScript string, so it will never be escaped in its canonical “code” representation. Therefore you will never make a string whose representation is '('.

If you mean a string of length 2, with the first character being a backslash, and the second one being a left parenthesis, then '\(' is what you want.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement