如何使用javascript上的替换将字符串上的括号转换为正则表达式?

时间:2017-06-13 09:44:23

标签: javascript regex replace

我想替换正则表达式(字符串),字符串是url(../img/content/blocks.svg) no-repeat scroll transparent;

我的脚本是

var content = content.replace(/url(../img/content/blocks.svg) no-repeat scroll transparent;/g, "none;");

这会出错:Uncaught SyntaxError: Unexpected identifier

1 个答案:

答案 0 :(得分:0)

在您的情况下,要替换的字符串是非正则表达式替换的完美候选者。遗憾的是,JavaScript缺少replaceAll,但您可以通过合并splitjoin来模仿它:

input.split('replace what').join('with what')

演示:

var content = 'some text url(../img/content/blocks.svg) no-repeat scroll transparent; some text url(../img/content/blocks.svg) no-repeat scroll transparent; some text';
var output = content.split('url(../img/content/blocks.svg) no-repeat scroll transparent;').join('none;');
console.log(output);