[SOLVED] RegExps + vars in JS

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

[SOLVED] RegExps + vars in JS

Post 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 :)
Last edited by Chris Corbyn on Fri Apr 01, 2005 3:57 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use the regexp objects.

Code: Select all

new RegExp( 'pattern', 'modifers' );
if I remember correctly.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Works a treat. Bit of a pain having to add all the backslashes to make it work though :-(
Post Reply