This is probably a very easy question to answer, but I haven’t figure out what is the correct way to do it.
I need to match a text via regular expressions using ^ and $ to only match elements starting and finishing with that string. However, I need to be able to do that using a variable:
var name // some variable
var re = new RegExp(name,"g");
So I’d like to match every string that includes completely (from beginning to end) my variable name, but I don’t want to match strings containing at some place my variable name.
How can I do it?
Thanks
The
iis for ignoring case.This matches just word “something” and will not match somethingelse.
if you are looking to match it in the middle of a sentence, you should use the following in your code
Alernately, using word boundaries,
Working Example