很奇怪为什么这不管用。我经历过许多不同的排列,但总的来说,这是我想要做的,但无法让它发挥作用。
基本上,使用散列键查找匹配项,然后使用该键获得带有散列引用的值。
var arr = {'blah':'WULLVERT','misc':'DUDETTER'};
var test_string = "maryLou is misc &&& such a cool mary blah dude yeah wullvert";
test_string = test_string.replace(/(jQuery.map(reg,function(k,v){return v}).join("|"))/gi,arr["$1"]);
test_string;当然,当我使用字符串的时候..。这是可行的:(尽管仍然不能使用$1作为哈希引用)。
test_string = test_string.replace(/(blah|msic)/gi,"$1_proofofconcept");发布于 2013-09-02 06:07:03
使用new RegExp(..)动态生成正则表达式对象。
var arr = {'blah':'WULLVERT','misc':'DUDETTER'};
var test_string = "maryLou is misc &&& such a cool mary blah dude yeah wullvert";
test_string.replace(
new RegExp(jQuery.map(arr,function(v,k){return k}).join("|"), 'gi'),
function(x) { return arr[x]; }
);
// => "maryLou is DUDETTER &&& such a cool mary WULLVERT dude yeah wullvert"https://stackoverflow.com/questions/18566520
复制相似问题