View Full Version : Whole word search using RegExp

05-16-2007, 03:44 AM
i want to do a whole word search...using this code:

x='mawmaw maw mawmaw'

regsearchtxt=new RegExp('\\W+maw\\W+','g')


and gladly it works...

but if used:

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?

Philip M
05-16-2007, 08:07 AM
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)) {

05-16-2007, 09:20 AM
thanks for the tip on word boundary...

i think this is the code:

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

Philip M
05-16-2007, 10:08 AM
Yes. But depends on whether you want e.g. maw! maw? "maw" or just maw to return true.

05-24-2007, 12:37 PM
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')

Philip M
05-24-2007, 03:29 PM
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.

05-27-2007, 03:00 PM
hm..yah, i tried it, and those MAWs were working ,hahaha...thanks by the way

09-03-2008, 11:37 AM
i'm just testing dis forum again

10-12-2008, 07:18 AM
testing this forum:)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum