您可以使用数组进行替换:
var array = {"from1":"to1", "from2":"to2"}
for (var val in array)
text = text.replace(array, array[val]);
Run Code Online (Sandbox Code Playgroud)
但是,如果你需要全局替换,即text = text.replace(/ from/g,"to"),该怎么办?
数组相当大,因此如果我为每个变量写"text = text.replace(...)",脚本将占用大量空间.
在这种情况下你如何使用数组?"/ from1/g":"to1"不起作用.