开发者

jQuery: create regex pattern from variable

开发者 https://www.devze.com 2022-12-21 17:45 出处:网络
I\'m trying to create a regex pattern out of a variable like: var tag = \"style\"; var pattern = \"/<\"+tag+\"[^>]*>((\\\\n|.)*)<\\\\/\"+tag+\">/开发者_StackOverflow社区gi\";

I'm trying to create a regex pattern out of a variable like:

var tag = "style";
var pattern = "/<"+tag+"[^>]*>((\\n|.)*)<\\/"+tag+">/开发者_StackOverflow社区gi";

but it won't work - anyone can tell me what's wrong?


Use the RegExp object

var tag = "style";
var pattern = new RegExp("<"+tag+"[^>]*>((\\n|.)*)<\\/"+tag+">","gi");


In general, matching html tags with regex isn't a good idea. See explanation here.


var re = new RegExp(string) ..

see here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号