RegExp: How to match on exact string only?

M

Mark Findlay

I am trying to figure out how to set up my reg exp search so that the search
will only match on the exact word.

Here is the current problem code:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

RegExp re = Word2;
if (re.Find(Word1))
{
bFound = TRUE;
}

Currently the bFound is set to TRUE since "Player.exe" is found within
"RealPlayer.exe". But I only want bFound to be TRUE is if the entire word
matches.

Can anyone assist with the correct syntax?

Many Thanks!

Mark
 
L

Lee

Mark Findlay said:
I am trying to figure out how to set up my reg exp search so that the search
will only match on the exact word.

Here is the current problem code:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

RegExp re = Word2;
if (re.Find(Word1))
{
bFound = TRUE;
}

Currently the bFound is set to TRUE since "Player.exe" is found within
"RealPlayer.exe". But I only want bFound to be TRUE is if the entire word
matches.

Can anyone assist with the correct syntax?

The correct syntax is:

if(Word1 == Word2)
{
bFound = true;
}


It's a waste of processor time to use regexp's for an exact match.
 
M

Mark Findlay

The sample us a bit simplified - the values being compared may or may not be
reg expressions so we have to assume that they will be. With this
assumption, can you recommend a regexp syntax that will match on the exact
word only?

Thanks,
Mark
 
L

Lee

Mark Findlay said:
The sample us a bit simplified - the values being compared may or may not be
reg expressions so we have to assume that they will be. With this
assumption, can you recommend a regexp syntax that will match on the exact
word only?


One other thing that should be clarified is that your
example code is not Javascript. Javascript doesn't
have type declarations, is case-sensitive, and the
RegExp object doesn't have a "find" method. Make sure
you're working in the correct language.

The solution to your problem seems to be to tie the
provided regular expression to the beginning and end
of the matching string:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

re = new RegExp("^"+Word2+"$");
bFound = re.test(Word1);
 
G

Grant Wagner

Mark said:
I am trying to figure out how to set up my reg exp search so that the search
will only match on the exact word.

Here is the current problem code:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

RegExp re = Word2;
if (re.Find(Word1))
{
bFound = TRUE;
}

Currently the bFound is set to TRUE since "Player.exe" is found within
"RealPlayer.exe". But I only want bFound to be TRUE is if the entire word
matches.

Can anyone assist with the correct syntax?

Many Thanks!

Mark

Word1 = "^Player.exe"; // "^" denotes it must match from the start of line
// or
Word1 = "^Player.exe$";
// "^" denotes it must match from the start of line
// "$" denotes it must match the end of line, in other words, an exact match

of course, as has been already pointed out, a regex match against "^...$" is the
same as testing if the two strings are equal.

Some regex references:

<url:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
/>
<url:
http://msdn.microsoft.com/library/en-us/jscript7/html/jsjsgrpregexpsyntax.asp?frame=true
/>

I prefer the Netscape one, because, although older, everything mentioned there
works in newer browsers. Whereas some of the newer features to Javascript (such
as "?" non-greedy and "(?:pattern)" non-capturing match) only work in the most
modern browsers.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
L

Lasse Reichstein Nielsen

of course, as has been already pointed out, a regex match against
"^...$" is the same as testing if the two strings are equal.

.... except when the string contains characters that are meaningfull in
RegExps. In this case RegExp("^Player.exe$") would also match
"Playerfexe". :)

Apart from that nitpick, I agree.

/L
 
S

Steve van Dongen

Mark Findlay said:
I am trying to figure out how to set up my reg exp search so that the search
will only match on the exact word.

Here is the current problem code:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

RegExp re = Word2;
if (re.Find(Word1))
{
bFound = TRUE;
}

Currently the bFound is set to TRUE since "Player.exe" is found within
"RealPlayer.exe". But I only want bFound to be TRUE is if the entire word
matches.

Can anyone assist with the correct syntax?

var re = /\bPlayer.exe\b/

http://msdn.microsoft.com/library/en-us/script56/html/js56jsgrpregexpsyntax.asp

\b
Matches a word boundary, that is, the position between a word and a
space. For example, 'er\b' matches the 'er' in "never" but not the
'er' in "verb".

Regards,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top