替换jQuery或javascript中出现的所有字符

replace all occurance of a character in jQuery or javascript

本文关键字:字符 jQuery javascript 替换      更新时间:2023-09-26

我想用'"代替"

我使用

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

现在我想只在"出现 '之前

时才这样做例如

我希望"title"被替换为'"title'"但是如果字符串已经是'"title'",我不想将"替换为'"

试试content.replace(/["]/g, "''$&") .

的例子:

var content = '"title", '"title", '"title'"';
content.replace(/["]/g, "''$&");
Output: ''"title'", '"title'", '"title'"'

希望有所帮助

content.replace(/(^|[^''])"/g, '$1''"');

好的,我想这对我有用

var content = 'hello"world'"'
content.replace(/''"/g,'"').replace(/"/g,'''"')
这个日志

"hello'"world'""