views:

640

answers:

1

I have the following code, which works, but I need to inject some different stuff into the regular expression object (regex2) at runtime. However, text.replace does not seem to like a string object for the regular expression, so how can I make this work?

var regex2 = /\|\d+:\d+/;
document.write("result = " + text.replace(regex2, '') + "<br>");
+9  A: 

You can make a regular expression object from a string using the RegExp constructor function:

var regExp = new RegExp(myString);  // regex pattern string

text.replace(regExp, '');
CMS