Ghilas BELHADJ
Ghilas BELHADJ

Reputation: 14096

create RegEx from variable

var re = "^abc";

How to create a RegEx from this variable to be able to test some strings with it like this:

/THE_re_VARIABLE_HERE/i.test( someString );

Upvotes: 0

Views: 61

Answers (2)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

You can use RegExp method:

var re = "^abc";
new RegExp(re,'i').test( someString );

See more about RegExp here

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80629

You'll have to use the class constructor:

var re = "^abc";
new RegExp( re, 'i' ).test( someString );

Upvotes: 1

Related Questions