Page 1 of 1

[SOLVED] RegExps + vars in JS

Posted: Thu Mar 31, 2005 7:44 pm
by Chris Corbyn
Hi,

Code: Select all

var testString = 'I\'m a String!';
var testVar = 'String';
var matches = testString.match(/I\'m a +testVar+/);
My question lies above... How can I get a JavaScript variable into a pattern?

Thanks :)

Posted: Thu Mar 31, 2005 8:25 pm
by feyd
use the regexp objects.

Code: Select all

new RegExp( 'pattern', 'modifers' );
if I remember correctly.

Posted: Fri Apr 01, 2005 1:50 am
by Chris Corbyn
feyd wrote:use the regexp objects.

Code: Select all

new RegExp( 'pattern', 'modifers' );
if I remember correctly.
Ahhhh.... RegExp objects eh :D I've never bother with them before, but it looks good to me.

Cheers :P

Posted: Fri Apr 01, 2005 3:55 am
by Chris Corbyn
Works a treat. Bit of a pain having to add all the backslashes to make it work though :-(