1

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

3 Answers 3

5
var strtomatch = "something";
var name = '^something$';
var re = new RegExp(name,"gi");
document.write(strtomatch.match(re));

The i is 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

var name = ' something ';

Alernately, using word boundaries,

var name = '\\bsomething\\b';

Working Example

Sign up to request clarification or add additional context in comments.

9 Comments

Not really. I want to match strings at the beginning of a line. I will clarify that point.
But I want to do that using a variable (in my example, var name).
Better, but that doesn't work. By the way, 'something' was just an example. Ideally, I would have something like name = '^' + name + '$'.
Thanks for your help. Almost there... why is this not working jsfiddle.net/Gpm2M/2 ?
@RobertSmith - That fiddle doesn't "work" because your regex has a space in it: /\b something/gi.
|
3

If you're saying you want to match something at the beginning or end of the string then do this:

/^something|something$/

With your variable:

new RegExp("^" + name + "|" + name + "$");

EDIT: For your updated question, you want the name variable to be the entire string matched, so:

new RegExp("^" + name + "$"); // note: the "g" flag from your question
                              // is not needed if matching the whole string

But that is pointless unless name contains a regular expression itself because although you could say:

var strToTest = "something",
    name = "something",
    re = new RegExp("^" + name + "$");

if (re.test(strToTest)) {
   // do something
}

You could also just say:

if (strToTest === name) {
   // do something
}

EDIT 2: OK, from your comment you seem to be saying that the regex should match where "something" appears anywhere in your test string as a discrete word, so:

"something else"           // should match
"somethingelse"            // should not match
"This is something else"   // should match
"This is notsomethingelse" // should not match
"This is something"        // should match
"This is something."       // should match?

If that is correct then:

re = new RegExp("\\b" + name + "\\b");

4 Comments

re = new RegExp("^" + name + "$") works fine, but I also want to match "... something ..."
OK, I've updated my answer with one last attempt to guess what you want. This whole process would've been so much easier if you'd included some concrete examples in your question, similar to what I've now added to my answer.
Yes, that's what I want. Seconds later than DarkXphenomenon's answer, but definitely +1 :-) Thanks for all your help.
You're welcome. Note that xdazz's answer basically said the same thing 28 minutes ago...
1

You should use /\bsomething\b/. \b is to match the word boundary.

"A sentence using something".match(/\bsomething\b/g);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.