0

I want to do some sort of searching with this:

if (v.name.search(new RegExp(/peter/i)) != -1) {
    console.log('found peter's record');
}

But how can I put a variable into that regular expression? I have tried with v.name.search(new RegExp('/'+var+'/i')), but it didn't work.

Thank you.

1
  • What are you expecting the variable to contain? A proper regex like [a-z] or peter? Commented Nov 11, 2013 at 6:40

1 Answer 1

3

That should be:

if (v.name.search(new RegExp(myVar, "i")) != -1) {
    console.log('found '+ myVar +' record');
}

See MDN for further info.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.