Qia*_*iao 6 javascript replace
您可以使用数组进行替换:
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"不起作用.
var array = {"from1":"to1", "from2":"to2"}
for (var val in array)
text = text.replace(new RegExp(val, "g"), array[val]);
Run Code Online (Sandbox Code Playgroud)
编辑:正如Andy所说,你可能不得不使用像这样的脚本来逃避特殊字符.
| 归档时间: |
|
| 查看次数: |
17631 次 |
| 最近记录: |