简体   繁体   English

jQuery字符串替换非常特殊的字符

[英]jQuery string replace very special characters

I have a PHP JSON encoded string like this:- 我有一个像这样的PHP JSON编码字符串:

SAINT JOHN\\'S 圣约翰

and I would like to change it to 我想将其更改为

SAINT JOHN'S in jQuery scripts. jQuery脚本中的SAINT JOHN'S。

I tried:- 我试过了:-

data[i].name.replace("[\']","'");
data[i].name.replace(/\'/g,"'");

all still give me SAINT JOHN\\'S. 所有人仍然给我圣约翰的。 how can I deal with the special character replacement? 如何处理特殊字符替换?

You have to escape the backslash \\ before single quote ' as well. 您还必须在单引号 '前转义反斜杠 \\

Live Demo 现场演示

data[i].name.replace(/\\\'/g,"'")

You can replace \\ with empty string but it could replace other back slash as well. 您可以将\\替换为空字符串,但也可以替换其他反斜杠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM