Referencing A Variable w/ Reg Expression..
Just not sure how to do this, but let's say I have:
var something=something.replace(pattern, "foobear");
would the part in blue work? I want to reference a variable in a regular expression. So what would be the proper syntax for this?
var pattern = new RegExp( "^" + foo );
var pattern = eval(/'^'+foo+'/gi');
this way you don't need to worry about escaping special characters.
So taking your advice towards this post, if I'm trying to replace the a in
Originally Posted by Teufel
with <span class="whatever">a</span> to produce,
I could target '>a' using a regular expression. So let's say that:
<span class="whatever">M</span><span class="whatever">a</span>xwell
Couldn't this be the appropriate regular expression?
Originally Posted by JohnBeason
Or, the one I wouldn't use would be
var exp = new RegExp(">"+the_letter, "ig");
The ig in both examples is optional, I'm just putting it there so you know where they go for either example.
var exp = eval("/>"+the_letter+"/ig;");
^ and $ mean at the start of the string, so /^>a$/ would only match the string ">a", and not "anything at all>aanything at all".
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)