/    Sign up×
Community /Pin to ProfileBookmark

How do I highlight only whole words in Javascript?

I have the following script which works well and highlights all occurrences of the search term wherever it appears in bodyText: beginning, middle, or end of words.

How do I go about limiting its hits to either words [B]beginning [/B]with the search term or only to [B]whole words [/B]matching the search term? Since I am dealing with whole term searching or term* (trailing wildcard) searching, I would like my highlighting to reflect the kind of search done.

Here’s my code so far:

[CODE]function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
{
// the highlightStartTag and highlightEndTag parameters are optional
if (searchTerm == ”)
{
return bodyText;
}
else
{
if ((!highlightStartTag) || (!highlightEndTag)) {
highlightStartTag = “<font style=’color:blue; background-color:yellow;’>”;
highlightEndTag = “</font>”;
}

// find all occurences of the search term in the given text,
// and add some “highlight” tags to them (we’re not using a
// regular expression search, because we want to filter out
// matches that occur within HTML tags, script blocks, and
// html entities, so we have to do a little extra validation)
var newText = “”;
var i = -1;
var lcSearchTerm = searchTerm.toLowerCase();
var lcBodyText = bodyText.toLowerCase();

while (bodyText.length > 0) {
i = lcBodyText.indexOf(lcSearchTerm, i+1);
if (i < 0) {
newText += bodyText;
bodyText = “”;
} else {
// skip anything inside an HTML tag
if (bodyText.lastIndexOf(“>”, i) >= bodyText.lastIndexOf(“<“, i)) {
// skip anything inside a <script> block
if (lcBodyText.lastIndexOf(“/script>”, i) >= lcBodyText.lastIndexOf(“<script”, i)) {
//skip anything inside an &…; html symbol block
if (lcBodyText.lastIndexOf(“;”, i) >= lcBodyText.lastIndexOf(“&”, i)) {
newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
bodyText = bodyText.substr(i + searchTerm.length);
lcBodyText = bodyText.toLowerCase();
i = -1;
}
}
}
}
}
return newText;
}
}
[/CODE]

I am planning to wrap this code with a wrapper that will allow one to pass it an id so the highlighting will only occur in the desired DOM elements. It looks really bad with the term occurs in your website’s menu and is therefore highlighted!

Terry

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ravish007Aug 21.2009 — Hi, I am looking for help with same problem. Did you find any way to do it yet?

Also, if you do write code to highlight by ID of element, would you mind sharing it with us here?

Thanks,

R
Copy linkTweet thisAlerts:
@treemonster19Sep 17.2009 — but how to pass body text in this function...i tried document.things like body.innerHTML or

document.body.text or

document.getElementByTagName('body').innerHTML etc do not work!!
×

Success!

Help @agrarian spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...