var txtpattern = '/[a-z]+/';
var regex = new RegExp(txtpattern);
var result = txtstring.match(regex); //returns null
var result = txtstring.match(/[a-z]+/); //returns some value
我的查询是,有没有办法在匹配参数中设置动态模式?
答案 0 :(得分:7)
使用new Regex()
时,您需要删除开头和结尾/
字符,如下所示:
var txtpattern = '[a-z]+';
var regex = new RegExp(txtpattern);
var result = txtstring.match(regex);