如何用一个双引号替换双引号,而不需要捕获双引号?

时间:2022-09-15 15:12:10

I'm trying to do a replace on a JSON string. Something like this:

我正在尝试对JSON字符串进行替换。是这样的:

'{"val1":"1","val2":"2"","val3":"","val4":""}'

Looking at the value tied to key "val2", I'm getting two double quotes together " 2 " ", but I can't fix that because that's how I get the JSON, I'm trying to replace those occurrences with 1 double quote: from "2"" to "2"

看看键"val2"上的值,我得到了两个双引号加在一起"2"但我无法修复它因为这就是我得到JSON的方式,我试图用一个双引号替换出现的值从"2"到"2"

I'm doing replace() but I can't find the right regex to do it. I have tried this:

我正在执行replace(),但是我找不到合适的regex。我试过这个:

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

If I do this it will replace "val3" and "val4" too.

如果我这样做,它也会取代“val3”和“val4”。

This one doesn't work:

这一不工作:

replace(/[^:]+"";/g, '"')

I'm trying to accomplish replace(REGEX_HERE): From "SomeCharactersHere"" to "SomeCharactersHere"

我正在尝试完成替换(REGEX_HERE):从“somecharacters shere”到“somecharacters shere”

3 个解决方案

#1


1  

Have you tried:

你有试过:

var s = '{"val1":"1","val2":"2"","val3":"","val4":""}';
s = s.replace(/([^:])\"\"/g, '$1\"')
console.log(s);

This matches some non-colon character followed by a set of double-double-quotes and replaces it with the originally found non-colon character along with a single-double-quotes.

这将匹配一些非冒号字符,后跟一组双引号,并用最初发现的非冒号字符和单双引号替换它。

Per a suggestion by ctwheels:

根据ctwheels的建议:

var s = '{"val1":"1","val2":"2"","val3":"","val4":""}';
s = s.replace(/([^:]")"+/g, '$1')
console.log(s);

As mentioned, it will capture the possibility of more than one extraneous double-quote and simplifies the replacement to just $1. Thanks!

如前所述,它将捕获不止一个无关的双引号的可能性,并将替换简化为1美元。谢谢!

#2


0  

Something like this should work.

像这样的东西应该有用。

const str = '{"val1":"1","val2":"2"","val3":"","val4":""}';
const cleaned = str.replace(/(\"+([^",]*)\"+)/g, (match,quotes,val) => `"${val}"`);
console.log(cleaned);
console.log(JSON.parse(cleaned));

Basically, it'll match each block of quotes (including extra quotes), then rewrite it to have only one pair of quotes.

基本上,它将匹配每个引号块(包括额外的引号),然后重写为只有一对引号。

Do note: it won't work if your values have commas. That's a limitation to this particular approach. If you need commas in the value, you could use this alternate regex:

注意:如果你的值有逗号,它就不会起作用。这就是这种方法的局限性。如果你需要逗号,你可以使用这个替代正则表达式:

/(\"([^"]*)\"+)/g

That one will work for commas, but won't work if you have a double-quote at the start of the string. You'd have to move the + in that case to the first one instead.

它可以用于逗号,但是如果在字符串的开头有一个双引号,它就不起作用。你必须把+移到第一个例子中。

#3


0  

I would add the comma in the match part.

我将在匹配部分中添加逗号。

'{"val1":"1","val2":"2"","val3":"","val4":""}'.replace(/"",/g, '\",');

I would also try to figure out why I'm getting badly formatted json :-)

我还想弄清楚为什么我的json格式很糟糕:-)

Better yet:

更好的是:

'{"val1":"1","val2":"2"","val3":"","val4":""}'.replace(/"\([^"]+\)""/g, '"\1"')

#1


1  

Have you tried:

你有试过:

var s = '{"val1":"1","val2":"2"","val3":"","val4":""}';
s = s.replace(/([^:])\"\"/g, '$1\"')
console.log(s);

This matches some non-colon character followed by a set of double-double-quotes and replaces it with the originally found non-colon character along with a single-double-quotes.

这将匹配一些非冒号字符,后跟一组双引号,并用最初发现的非冒号字符和单双引号替换它。

Per a suggestion by ctwheels:

根据ctwheels的建议:

var s = '{"val1":"1","val2":"2"","val3":"","val4":""}';
s = s.replace(/([^:]")"+/g, '$1')
console.log(s);

As mentioned, it will capture the possibility of more than one extraneous double-quote and simplifies the replacement to just $1. Thanks!

如前所述,它将捕获不止一个无关的双引号的可能性,并将替换简化为1美元。谢谢!

#2


0  

Something like this should work.

像这样的东西应该有用。

const str = '{"val1":"1","val2":"2"","val3":"","val4":""}';
const cleaned = str.replace(/(\"+([^",]*)\"+)/g, (match,quotes,val) => `"${val}"`);
console.log(cleaned);
console.log(JSON.parse(cleaned));

Basically, it'll match each block of quotes (including extra quotes), then rewrite it to have only one pair of quotes.

基本上,它将匹配每个引号块(包括额外的引号),然后重写为只有一对引号。

Do note: it won't work if your values have commas. That's a limitation to this particular approach. If you need commas in the value, you could use this alternate regex:

注意:如果你的值有逗号,它就不会起作用。这就是这种方法的局限性。如果你需要逗号,你可以使用这个替代正则表达式:

/(\"([^"]*)\"+)/g

That one will work for commas, but won't work if you have a double-quote at the start of the string. You'd have to move the + in that case to the first one instead.

它可以用于逗号,但是如果在字符串的开头有一个双引号,它就不起作用。你必须把+移到第一个例子中。

#3


0  

I would add the comma in the match part.

我将在匹配部分中添加逗号。

'{"val1":"1","val2":"2"","val3":"","val4":""}'.replace(/"",/g, '\",');

I would also try to figure out why I'm getting badly formatted json :-)

我还想弄清楚为什么我的json格式很糟糕:-)

Better yet:

更好的是:

'{"val1":"1","val2":"2"","val3":"","val4":""}'.replace(/"\([^"]+\)""/g, '"\1"')