Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have to build a RegExp obejct, that will search words from an array,
and will find only whole words match.

e.g.
I have a words array ('a','b','c'),
and I want the RegExp to find 'a' or 'b' or 'c', but not 'aaa'.

How should I build my RegExp?
Thanks in advance!

What I have tried:

I tried this code:
JavaScript
new RegExp('(word1|word2|word3)','g');


It works well, but it find also words like "word11111", I have to match only the whole words.
Posted
Updated 11-Jan-17 22:33pm

Try:
JavaScript
new RegExp('\\b(word1|word2|word3)\\b','g');
\b is a word boundary[^] so now your regex will only match full words.

To create this from an array with words:
JavaScript
var words = [ "word1", "word2", "word3" ];
var regex = new RegExp('\\b(' + words.join('|') + ')\\b', 'g');
 
Share this answer
 
Comments
Member 11736781 12-Jan-17 3:12am    
Thank you very much for your repley!
When I use:
var regex = new RegExp('(' + words.join('|') + ')', 'g');
The regex find also partial words,
but when I use:
var regex = new RegExp('\\b(' + words.join('|') + ')\\b', 'g');
The regex doesn't find any word :-(
Member 11736781 12-Jan-17 3:43am    
Now I see that with english characters the code works well, but not with hebrews characters.
Thomas Daniels 12-Jan-17 10:15am    
I see. Can't help with that, sorry! But I'm glad to hear that you found a solution yourself.
The solution was to use XRegExp:

var re = XRegExp('(^|[^_0-9\\pL])(' + matchWords.join('|') + ')(?![_0-9\\pL])','ig');
text = XRegExp.replace(text.replace(/\n$/g, '\n\n'), re, '$1<mark>$2</mark>');
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900