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?

1
  • 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. Commented Mar 4, 2010 at 0:44

3 Answers 3

5

Use the RegExp object

var tag = "style";
var pattern = new RegExp("<"+tag+"[^>]*>((\\n|.)*)<\\/"+tag+">","gi");
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

var re = new RegExp(string) ..

see here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.