chopper draw lion4
chopper draw lion4

Reputation: 13497

How do I include symbols in regular expression constructors?

I want the following regular expression:

 /(ending)$/

Where ending is a variable. I discovered that to use variables with regular expressions I must use regular expression constructors. So I tried:

var pattern = new RegExp((ending)$);

But this does not work either! This works if I do not include the grouping parenthesis and dollar sign, but I need those special characters as part of my pattern!

I tried to wrap the special characters in quotations, and also cancel them out by using a backslash, but nothing seems to work! What should I do to include special characters in my regular expression constructor?!

Upvotes: 0

Views: 61

Answers (1)

Keith Nicholas
Keith Nicholas

Reputation: 44298

it takes a string...

var pattern = new RegExp("(" + ending+ ")$");

Upvotes: 4

Related Questions