我希望用"ad231-3213-e12211"替换"[ID]"。
我使用了以下正则表达式:/\[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);
};
而且它们都不起作用,我缺少什么?