Javascript regexp:在反向引用模式中使用变量?
我有一个模式来查找查询字符串中的匹配项:
'url.com/foo/bar?this=that&thing=another'.replace(/(thing=)([^&]*)/, '$1test')
我希望能够做的是使用变量值作为匹配的参数,如:
'url.com/foo/bar?this=that&thing=another'.replace('/(' + key + '=)([^&]*)/', '$1test')
[edit]以下是代码使用方式的上下文:
GetSrcParam: function(key, value) {
var newSrc = $(this._image).attr('src'),
pattern = '(' + key + '=)([^&]*)';
if (newSrc.match(pattern) == null)
newSrc += '&' + key + '=' + value;
else
newSrc = newSrc.replace(newSrc, '$1' + value);
return newSrc;
}
但它没有按预期工作 - 任何人都可以帮忙吗?
没有找到相关结果
已邀请:
1 个回复
部窖空
你是否意识到这也与
中的
相符?要避免这种情况,请添加单词边界锚点: