RegExp: How to match on exact string only?

Discussion in 'Javascript' started by Mark Findlay, May 14, 2004.

  1. Mark Findlay

    Mark Findlay Guest

    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
     
    Mark Findlay, May 14, 2004
    #1
    1. Advertising

  2. Mark Findlay

    Lee Guest

    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.
     
    Lee, May 14, 2004
    #2
    1. Advertising

  3. Mark Findlay

    Mark Findlay Guest

    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.
    >
     
    Mark Findlay, May 14, 2004
    #3
  4. Mark Findlay

    Lee Guest

    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);
     
    Lee, May 14, 2004
    #4
  5. Mark Findlay

    Grant Wagner Guest

    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/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 <>

    * 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
     
    Grant Wagner, May 14, 2004
    #5
  6. 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.'
     
    Lasse Reichstein Nielsen, May 14, 2004
    #6
  7. "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-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
     
    Steve van Dongen, May 16, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    2
    Views:
    321
    John Harrison
    Sep 10, 2005
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    524
    Mikel Lindsaar
    Mar 31, 2008
  3. Old Echo
    Replies:
    1
    Views:
    194
    Adam Shelly
    Sep 4, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    391
    7stud --
    Aug 21, 2009
  5. Uldis  Bojars
    Replies:
    2
    Views:
    204
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page