Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using the value of a variable in a regex?

Status
Not open for further replies.

raindogs

Programmer
Nov 23, 2005
27
US
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
 
[tt]checkFor=new RegExp("\\d"+myVariable)[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top