0

I know that this's been asked before but none of the suggested solutions worked for me. I've got the following:

  var regex = new RegExp(/D0030001 IN/gi);
  $("p").filter(function () {
        return regex.test(this.id); 
   }).css("background-color","blue");

This one works fine. However, when I try to do

 spl = [];
 spl[0] = "D0030001";
 spl[1] = "IN";
 var regex = new RegExp("/" + spl[0] + " " + spl[1] + "/gi");
       $("p").filter(function () {
        return regex.test(this.id); 
   }).css("background-color","blue");

This one doesn't work. In other words I need to use variables to construct the regex pattern. Thanks

0

1 Answer 1

2

You need to use the second argument of RegExp constructor to set the flags and you don't need / / delimiters.

var regex = new RegExp(spl[0] + " " + spl[1], "gi");
Sign up to request clarification or add additional context in comments.

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.