Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Whole word search using RegExp

    i want to do a whole word search...using this code:

    Code:
    <script>
    x='mawmaw maw mawmaw'
    
    
    regsearchtxt=new RegExp('\\W+maw\\W+','g')
    
    
    if(x.search(regsearchtxt)!=-1){alert('h')}
    </script>
    and gladly it works...

    but if used:

    Code:
    x='maw mawmaw'
    it won't...using ^ might be the solution....but it solves the problem but creates bigger problems... same also for x='mawmaw maw'

    any suggestions?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,729
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    If you are trying to match 'maw' as a whole word (only) then you could use the \b switch (word boundary)


    x = 'mawmaw maw maw'
    y = x.replace(/[^A-z\s]/g,"") // delete non-alpha characters, retain spaces
    if (/maw\b/gi.test(y.value)) {

  • #3
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks for the tip on word boundary...

    i think this is the code:

    Code:
    regsearchtxt=new RegExp('\\bmaw\\b','g')

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,729
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    Yes. But depends on whether you want e.g. maw! maw? "maw" or just maw to return true.

  • #5
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hey...i think i need that maw! maw?
    so should my code have...i think some additions like this:

    regsearchtxt=new RegExp('\\b|\?|!maw\\b|\?|!','g')

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,729
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    In fact your regex
    regsearchtxt=new RegExp('\\bmaw\\b','g')
    will do fine.

    It seems that \b ignores punctuation, quotes and ! ? symbols. I did not know this before, but in fact it makes sense, as otherwise maw! maw? maw. maw, and "maw" would return false.

  • #7
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hm..yah, i tried it, and those MAWs were working ,hahaha...thanks by the way

  • #8
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i'm just testing dis forum again
    Last edited by ken_shoti; 09-03-2008 at 11:37 AM. Reason: :)

  • #9
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    testing this forum


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •