在正则表达式中传递变量

时间:2018-11-24 15:18:33

标签: javascript jquery regex express expression

我要在表达式内传递变量吗?

我的霸王龙是:

/([01])010010/g

要在正则表达式中传递变量STR而不是010010。怎么了?

是这样的:

var str = 010010;
/([01]) + STR + /g

完整代码:

let a ="1111111010011001011001100111111111010011100111010001011110011101000100010110010101000011111000011110100101010111111010010011111";

let matches = [];
var STR = '010010';

a.replace(/([01])010010/g,(_,charbefore)=>{
 matches.push(charbefore);
});
console.log(matches);

查看: http://jsfiddle.net/a4rbu1vy/

0 个答案:

没有答案