javascript 使用 jquery 将两个双引号替换为单个双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4946610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:26:15  来源:igfitidea点击:

Replace two double quotes with single one using jquery

javascriptjqueryregexreplace

提问by Prasad

How do I replace two double quotes into single one using jQuery?

如何使用 jQuery 将两个双引号替换为单引号?

For example: I need to replace

例如:我需要更换

Sam is at his ""Home""

山姆在他的“家”

to

Sam is at his "Home"

山姆在他的“家”

Looking for something similar to this regex (which replaces double quotes to single quotes):

寻找类似于此正则表达式的东西(将双引号替换为单引号):

mystring.replace(/"/g, "'")

回答by Kobi

try this:

试试这个:

mystring = mystring.replace(/""/g, '"');

The regex captures two double quotes and replaces them with one. We're using a regex to replace more than a single match (JavaScript's replace will only replace the first one).

正则表达式捕获两个双引号并将它们替换为一个。我们使用正则表达式来替换多个匹配项(JavaScript 的替换只会替换第一个匹配项)。

Note that the second argument to replace is a string. To represent "in a string we need to escape it: "\"", or use a single quote string: '"'. JavaScript supports both.

请注意,要替换的第二个参数是一个字符串。要"在字符串中表示,我们需要对其进行转义:"\"",或使用单引号字符串:'"'。JavaScript 两者都支持。