Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > RegExp: How to match on exact string only?

Reply
Thread Tools

RegExp: How to match on exact string only?

 
 
Mark Findlay
Guest
Posts: n/a
 
      05-14-2004
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



 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      05-14-2004
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.

 
Reply With Quote
 
 
 
 
Mark Findlay
Guest
Posts: n/a
 
      05-14-2004
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

"Lee" <> wrote in message
news:...
> 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.
>



 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      05-14-2004
Mark Findlay said:

>> >Here is the current problem code:
>> >
>> >Word1 = "RealPlayer.exe"
>> >Word2 = "Player.exe"
>> >
>> >RegExp re = Word2;
>> >if (re.Find(Word1))
>> >{
>> > bFound = TRUE;
>> >}


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

 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      05-14-2004
Mark Findlay wrote:

> 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/...ce/frames.html
/>
<url:
http://msdn.microsoft.com/library/en...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 "(?attern)" non-capturing match) only work in the most
modern browsers.

--
| Grant Wagner <>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html


 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      05-14-2004
Grant Wagner <> writes:

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


> 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
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Steve van Dongen
Guest
Posts: n/a
 
      05-16-2004
"Mark Findlay" <> wrote:

>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...gexpsyntax.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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Exact match with regular expression Mr.SpOOn Python 4 11-04-2008 08:59 PM
Exact Match and Identity conversion Neelesh Bodas C++ 17 08-14-2007 11:49 PM
some problems about exact match in the overload resolution. Wayne Shu C++ 2 07-29-2007 07:21 PM
conversion, promotion, exact match Andy C++ 2 09-10-2005 01:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57