Fuxi
Fuxi

Reputation: 7599

jQuery: create regex pattern from variable

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

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

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

Upvotes: 2

Views: 7134

Answers (3)

Scott Evernden
Scott Evernden

Reputation: 39926

var re = new RegExp(string) ..

see here

Upvotes: 0

PetersenDidIt
PetersenDidIt

Reputation: 25620

Use the RegExp object

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

Upvotes: 5

James Kolpack
James Kolpack

Reputation: 9382

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

Upvotes: 1

Related Questions