0

if, a = "Bob\'s house"

how would you replace the backslash with another '? I want a to equal "Bob''s house"

I would assume that I can do a.replace("\", "'") but this doesn't work

Eric Chu
  • 1,599
  • 3
  • 17
  • 26

3 Answers3

0

It does not work because \' is treated as a single character. In Javascript \ is used to initiate an escape character, which means "literally the character after \".

In order to show \ you therefore need to write \\. And if you want to change escaped ' into '' what you need to do is simply a.replace("\'", "\'\'");

H. Tugkan Kibar
  • 2,215
  • 13
  • 17
0

basically a = "Bob\'s house" is interpreted as "Bob's house", youre just escaping the '. there is no backslash in the string.

to have your wanted result ("Bob''s house"), simply do the following:

a.replace("\'", "\'\'")
Bamieh
  • 9,317
  • 4
  • 30
  • 51
0

as user2182349 said, a.repalce("\\", "'") did the trick

Eric Chu
  • 1,599
  • 3
  • 17
  • 26