Reputation: 10246
How to search a word in exact match, for example how to search word 'leak
', but search results should not include words like 'leaked
' or 'leaks
' etc.
Help in both Javascript and MySQL would be appreciated.
Upvotes: 1
Views: 708
Reputation: 4224
\bleak\b
I've tested it on the following inputs:
Upvotes: 0
Reputation: 25874
The regex would be
/\bleak\b/
Usage in JavaScript
var patt1=/\bleak\b/i;
patt1.test(your_string); // Returns true/false
For MySQL (I didn't know it had support for regexes, thanks Rohan Prabhu):
expr REGEXP [[:<:]]leak[[:>:]]
Upvotes: 1
Reputation: 7302
Basically, you need to look for word boundaries. So don't think of looking for the word leak
, but looking for the word $leak#
, where $
is an imaginary character that is at the beginning of every word and #
is an imaginary character at the end of every word (Not a standard convention, just assume it for this example). The words are delimited, by, lets say whitespaces for now. So, leaked
and leaks
is $leaked#
and $leaks#
. As such, the only part that matches is $leak
and not $leak#
.
To do that in MySQL, the condition would be:
REGEXP '[[:<:]]leak[[:>:]]';
Here, [[:<:]]
is the begin-word word-boundary and [[:>:]]
is the end-word word-boundary.
For JavaScript, the query would be:
/\bleak\b/
Here, the sequence \b
represents a word-boundary (in javascript, there is no distinction between begin-word and end-word boundaries). It won't only work for whitespaces, but even punctuation characters, for example:
.leak
,leak
;leak
will match /\bleak\b/
, but aleak
will not. (I am unsure about MySQL though)
Upvotes: 4
Reputation: 20431
This will match words that don't start with leak*
(?!leak\w*)\b\w+\b
Matches in bold:
Upvotes: 0