This question already has an answer here:
这个问题在这里已有答案:
- Is there a RegExp.escape function in Javascript? 12 answers
Javascript中是否有RegExp.escape函数? 12个答案
I want to replace a regular expression (a string), the string is url(../img/content/blocks.svg) no-repeat scroll transparent;
我想替换正则表达式(字符串),字符串是url(../ img / content / blocks.svg)no-repeat scroll transparent;
My script is
我的剧本是
var content = content.replace(/url(../img/content/blocks.svg) no-repeat scroll transparent;/g, "none;");
This makes an error : Uncaught SyntaxError: Unexpected identifier
这会产生错误:Uncaught SyntaxError:意外的标识符
1 个解决方案
#1
0
The string to be replaced is a perfect candidate for non-regex based replacement in your case. Unfortunately, JavaScript lacks replaceAll
, but you may mimic it by combining split
and join
:
在您的情况下,要替换的字符串是非正则表达式替换的完美候选者。不幸的是,JavaScript缺少replaceAll,但你可以通过组合split和join来模仿它:
input.split('replace what').join('with what')
Demo:
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);
#1
0
The string to be replaced is a perfect candidate for non-regex based replacement in your case. Unfortunately, JavaScript lacks replaceAll
, but you may mimic it by combining split
and join
:
在您的情况下,要替换的字符串是非正则表达式替换的完美候选者。不幸的是,JavaScript缺少replaceAll,但你可以通过组合split和join来模仿它:
input.split('replace what').join('with what')
Demo:
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);