admin管理员组

文章数量:1325756

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 pletely (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

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 pletely (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

Share Improve this question edited Jul 15, 2012 at 4:34 r_31415 asked Jul 15, 2012 at 4:19 r_31415r_31415 8,98218 gold badges80 silver badges123 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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

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 ment 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");

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

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

本文标签: javascriptMatching string using variable in regular expression withand Stack Overflow