2

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?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Fuxi
  • 7,611
  • 25
  • 93
  • 139
  • 1
    What are you trying to accomplish? There's probably a better way to find the html tag that you're looking for using jquery selectors. – James Kolpack Mar 04 '10 at 00:44

3 Answers3

5

Use the RegExp object

var tag = "style";
var pattern = new RegExp("<"+tag+"[^>]*>((\\n|.)*)<\\/"+tag+">","gi");
PetersenDidIt
  • 25,562
  • 3
  • 67
  • 72
1

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

Community
  • 1
  • 1
James Kolpack
  • 9,331
  • 2
  • 44
  • 59
0

var re = new RegExp(string) ..

see here

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84