-2

我必须用(一个引号)替换""(两个引号")。我用了:

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

但如果它有""""(四个引号),它会用"(一个引号)替换它再次用(一个引号)替换""(两个引号")。

我需要""""(四个引号)应该替换为""(两个引号)

4

2 回答 2

2

您在问题中提供的代码已经完全按照您所说的做:

'I like """"orange"""" and ""apple""'.replace(/""/g,'"');
// Returns:
'I like ""orange"" and "apple"'

'""'.replace(/""/g,'"');
// Returns:
'"'

'""""'.replace(/""/g,'"');
// Returns:
'""'

除非您在问题中遗漏了某些信息,否则没有什么可以解决的。

出于这个原因,我相信问题出在您的代码中的其他地方。

于 2013-02-07T10:59:04.167 回答
0

如果你想用两个引号替换四个引号,这样做:

var string = "\"\"\"\"";
string = string.replace("\"\"\"\"", "\"\"")
于 2013-02-07T10:45:55.280 回答