在字符串中替换所有双引号

时间:2022-09-15 15:34:48

I am retrieving data from database where a field contains String with HTML data. I want to replace all the double quotes such that it can be used for parseJSON of jQuery.

我正在从数据库中检索数据,其中字段包含带有HTML数据的字符串。我想要替换所有双引号,这样它就可以用于jQuery的parseJSON。

Using java i am trying to replace the quotes using..

使用java,我试图用。

details.replaceAll("\"","\\\"");
  //details.replaceAll("\"","&quote;"); details.replaceAll("\"","&#34");

Have tried al lthis way but the resultant string doesn't show the change. O'reily article prescribes Article apache string utils. Is there any other way??

已经尝试过这种方法,但是合成的字符串没有显示变化。O'reily的文章描述了apache字符串utils。还有别的办法吗?

Any regex or something?

任何正则表达式还是什么?

7 个解决方案

#1


82  

Here's how

这是如何

String details = "Hello \"world\"!";
details = details.replace("\"","\\\"");
System.out.println(details);               // Hello \"world\"!

Note that strings are immutable, thus it is not sufficient to simply do details.replace("\"","\\\""). You must reassign the variable details to the resulting string.

注意,字符串是不可变的,因此仅仅做一些细节是不够的。必须将变量细节重新分配给结果字符串。


Using

使用

details = details.replaceAll("\"","&quote;");

instead, results in

相反,结果

Hello &quote;world&quote;!

#2


27  

Would not that have to be:

这不是必须的:

.replaceAll("\"","\\\\\"")

FIVE backslashes in the replacement String.

替换字符串中的五个反斜杠。

#3


5  

I think a regex is a little bit of an overkill in this situation. If you just want to remove all the quotes in your string I would use this code:

我认为在这种情况下,regex有点过头了。如果你想删除字符串中的所有引号,我将使用以下代码:

details = details.replace("\"", "");

#4


4  

To make it work in JSON, you need to escape a few more character than that.

要使它在JSON中工作,您需要再转义几个字符。

myString.replace("\\", "\\\\")
    .replace("\"", "\\\"")
    .replace("\r", "\\r")
    .replace("\n", "\\n")

and if you want to be able to use json2.js to parse it then you also need to escape

如果你想使用json2。要解析它,还需要转义

   .replace("\u2028", "\\u2028")
   .replace("\u2029", "\\u2029")

which JSON allows inside quoted strings, but which JavaScript does not.

在引用的字符串中允许使用哪个JSON,但是哪个JavaScript不允许。

#5


3  

I know the answer is already accepted here, but I just wanted to share what I found when I tried to escape double quotes and single quotes.

我知道答案已经在这里被接受了,但是我只是想分享我在试图逃避双引号和单引号时发现的东西。

Here's what I have done: and this works :)

以下是我所做的:

to escape double quotes:

为了逃避双引号:

    if(string.contains("\"")) {
        string = string.replaceAll("\"", "\\\\\"");
    }

and to escape single quotes:

为了避免使用单引号:

    if(string.contains("\'")) {
        string = string.replaceAll("\'", "\\\\'");
    }

PS: Please note the number of backslashes used above.

请注意上面使用的反斜杠的数量。

#6


2  

This is to remove double quotes in a string.

这是为了删除字符串中的双引号。

str1 = str.replace(/"/g, "");
alert(str1);

#7


0  

The following regex will work for both:

以下regex将同时适用于这两种情况:

  text = text.replaceAll("('|\")", "\\\\$1");

#1


82  

Here's how

这是如何

String details = "Hello \"world\"!";
details = details.replace("\"","\\\"");
System.out.println(details);               // Hello \"world\"!

Note that strings are immutable, thus it is not sufficient to simply do details.replace("\"","\\\""). You must reassign the variable details to the resulting string.

注意,字符串是不可变的,因此仅仅做一些细节是不够的。必须将变量细节重新分配给结果字符串。


Using

使用

details = details.replaceAll("\"","&quote;");

instead, results in

相反,结果

Hello &quote;world&quote;!

#2


27  

Would not that have to be:

这不是必须的:

.replaceAll("\"","\\\\\"")

FIVE backslashes in the replacement String.

替换字符串中的五个反斜杠。

#3


5  

I think a regex is a little bit of an overkill in this situation. If you just want to remove all the quotes in your string I would use this code:

我认为在这种情况下,regex有点过头了。如果你想删除字符串中的所有引号,我将使用以下代码:

details = details.replace("\"", "");

#4


4  

To make it work in JSON, you need to escape a few more character than that.

要使它在JSON中工作,您需要再转义几个字符。

myString.replace("\\", "\\\\")
    .replace("\"", "\\\"")
    .replace("\r", "\\r")
    .replace("\n", "\\n")

and if you want to be able to use json2.js to parse it then you also need to escape

如果你想使用json2。要解析它,还需要转义

   .replace("\u2028", "\\u2028")
   .replace("\u2029", "\\u2029")

which JSON allows inside quoted strings, but which JavaScript does not.

在引用的字符串中允许使用哪个JSON,但是哪个JavaScript不允许。

#5


3  

I know the answer is already accepted here, but I just wanted to share what I found when I tried to escape double quotes and single quotes.

我知道答案已经在这里被接受了,但是我只是想分享我在试图逃避双引号和单引号时发现的东西。

Here's what I have done: and this works :)

以下是我所做的:

to escape double quotes:

为了逃避双引号:

    if(string.contains("\"")) {
        string = string.replaceAll("\"", "\\\\\"");
    }

and to escape single quotes:

为了避免使用单引号:

    if(string.contains("\'")) {
        string = string.replaceAll("\'", "\\\\'");
    }

PS: Please note the number of backslashes used above.

请注意上面使用的反斜杠的数量。

#6


2  

This is to remove double quotes in a string.

这是为了删除字符串中的双引号。

str1 = str.replace(/"/g, "");
alert(str1);

#7


0  

The following regex will work for both:

以下regex将同时适用于这两种情况:

  text = text.replaceAll("('|\")", "\\\\$1");