我想替换这样的:这个 “ad231-3213-e12211” “[ID]”JavaScript的正则表达式支架
我用正则表达式:/\[ID\]/i
而且它与.replace(/\[ID\]/i, id)
现在我封装它在这个函数:
self.changeUrl = function (url, id, replaceExp) {
replaceExp = replaceExp === 'undefined' ? new RegExp("\[ID\]") : replaceExp instanceof RegExp ? replaceExp : new RegExp("\[ID\]"); // basically ensure that this is a regexp
return url.replace(replaceExp, id);
};
这:
self.changeUrl = function (url, id, replaceExp) {
replaceExp = replaceExp === 'undefined' ? new RegExp("\u005BID\u005D") : replaceExp instanceof RegExp ? replaceExp : new RegExp("\u005BID\u005D"); // basically ensure that this is a regexp
return url.replace(replaceExp, id);
};
而他们都没有工作,我失踪了什么?
使用'“\\ [ID \\]”' – hjpotter92
谢谢,后一个答案,为什么你要做到这一点,我将接受它 –