Click to See Complete Forum and Search --> : using the value of a variable in a regex?


raindogs
05-08-2006, 04:24 PM
I'm trying to modify a string with a piece of javascript, but I'd like to include a the value of a variable in a search string. I have it set up something like this:

var checkFor = /\d/;
var replaceString = 'replacewiththis';
var postRegex = myString.replace(checkFor, replaceString);

The code above works just fine for just looking for digits, but things break down if I try to add the value of a variable, i.e.,

var checkFor = /\d/ + myVariable;

Does anyone know how to do this?

Thanks,
Alex

Charles
05-08-2006, 04:36 PM
var reFlags = ''
var checkFor = new RegExp ('\d' + myVariable, reFlags)