Previous answers here did not take into account the requirements of the whole word. In fact, this is difficult to achieve, since the word boundary \b does not support word boundaries with neighboring Unicode characters in Hebrew, which we can only match with a character class using the notation \u .
I suggest using search commands and capture groups to make sure we capture the whole Hebrew word ( (^|[^\u0590-\u05FF])([\u0590-\u05FF]+)(?![\u0590-\u05FF]) , which ensures that there is a non-Jewish symbol or the beginning of a line before the Hebrew word - add \s if there are spaces between Hebrew words!) and \b[az\s]+\b to match a sequence of whole English words separated by spaces .
If you plan to embed <span> tags in a sentence around whole words, here is a function that might help:
var str = 'so היי all whats up אתכם?';
span { background:#FFCCCC; border:1px solid #0000FF; }
<div width="645" id="r"/>
Result:
<span>so</span><span>היי</span><span>all whats up</span><span>אתכם</span>?
If you don't need any punctuation marks or alphanumeric objects in your release, just combine all the words in English and Hebrew, and then use
var str = 'היי, User234, so 222היי all whats up אתכם?'; var re = /(^|[^\u0590-\u05FF])([\u0590-\u05FF]+)(?![\u0590-\u05FF])|(\b[az\s]+\b)/ig; var res = []; while ((m = re.exec(str)) !== null) { if (m.index === re.lastIndex) { re.lastIndex++; } if (m[1] !== undefined) { res.push('<span>'+m[2].trim()+'</span>'); } else { res.push('<span>'+m[3].trim()+'</span>'); } } document.getElementById("r").innerHTML = res.join("");
span { background:#FFCCCC; border:1px solid #0000FF; }
<div width="645" id="r"/>
Result:
<span>היי</span><span>so</span><span>היי</span><span>all whats up</span><span>אתכם</span>
Wiktor stribiżew
source share