我好像有问题。我有一个查询字符串,其值可以包含单引号。这将破坏查询字符串。所以我试图进行替换以更改'
为\'
.
这是一个示例代码:
"This is' it".replace("'", "\'");
输出仍然是:
"This is' it".
它认为我只是在为引用做一个转义字符。
所以我尝试了这两段代码:
"This is' it".replace("'", "\\'"); // \\ for the backslash, and a ' char
"This is' it".replace("'", "\\\'"); // \\ for the backslash, and \' for the ' char
以上两个仍然导致相同的输出:
"This is' it"
我似乎只能让这个实际吐出一个斜线:
"This is' it".replace("'", "\\\\'");
结果是:
"This is\\' it"
有什么建议么?我只想将 a 替换'
为\'
.
看起来应该没那么难。