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

r_31415
  • 8,752
  • 17
  • 74
  • 121

3 Answers3

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

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • Not really. I want to match strings at the beginning of a line. I will clarify that point. – r_31415 Jul 15 '12 at 04:24
  • But I want to do that using a variable (in my example, var name). – r_31415 Jul 15 '12 at 04:27
  • Better, but that doesn't work. By the way, 'something' was just an example. Ideally, I would have something like name = '^' + name + '$'. – r_31415 Jul 15 '12 at 04:30
  • Thanks for your help. Almost there... why is this not working http://jsfiddle.net/Gpm2M/2/ ? – r_31415 Jul 15 '12 at 04:41
  • @RobertSmith - That fiddle doesn't "work" because your regex has a space in it: `/\b something/gi`. – nnnnnn Jul 15 '12 at 04:44
  • Sure, I put it there. I also want to match that and also "something ". – r_31415 Jul 15 '12 at 04:45
  • @RobertSmith - If there's a space in your regex it _won't_ match strings that don't have a space in them. And you're testing a string that doesn't have a space. In your case you've also got a `\b` word boundary match _before_ the space, so it will only match against a string that ends with a word boundary then a space then "something". – nnnnnn Jul 15 '12 at 04:46
  • You were not escaping the \ character in that string. That is why the older fiddle was not working. This one is correct. – Anirudh Ramanathan Jul 15 '12 at 04:48
  • I think I was escaping it: name = '\\b' + string + '$' but I got confused between strtomatch and string :-) – r_31415 Jul 15 '12 at 04:52
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");
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • re = new RegExp("^" + name + "$") works fine, but I also want to match "... something ..." – r_31415 Jul 15 '12 at 04:44
  • 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. – nnnnnn Jul 15 '12 at 04:53
  • Yes, that's what I want. Seconds later than DarkXphenomenon's answer, but definitely +1 :-) Thanks for all your help. – r_31415 Jul 15 '12 at 04:55
  • You're welcome. Note that xdazz's answer basically said the same thing 28 minutes ago... – nnnnnn Jul 15 '12 at 05:00
1

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

"A sentence using something".match(/\bsomething\b/g);
xdazz
  • 158,678
  • 38
  • 247
  • 274