This question already has answers here:
How to replace all occurrences of a string?
                            
                                (63个答案)
                            
                    
                6年前关闭。
        

    

我想用单引号替换所有出现的字符串,但用str.replace只能替换脚本的第一次出现:

"7<singleQuote>1 inche<singleQuote>s"



var data = "7<singleQuote>1 inche<singleQuote>s"
var output = data.replace("<singleQuote>","'")


输出:7'1 inche<singleQuote>s

我想用<singleQuote>替换'

最佳答案

将正则表达式与g标志一起使用:

var output = data.replace(/<singleQuote>/g, "'");


MDN:String.prototype.replace

09-25 16:14