Search Result Keyword Highlighter
Highlight a keyword in the body of text for a given search result.
Why?
People who use search want to have the word(s) they searched for highlighted in the results. We know this. (because Google does it)
Google have user-tested every feature of their site/experience...
What?
For a given set of keywords that a person is searching for, we highlight all occurrences of the word in the text of the search results.
Features?
Case-insentive Keyword Highlighting
a search for Java
, javA
or java
will highlight JAVA
or JaVa
in the search result.
Finds Multiple Occurrences of the Same Keyword in Text
Example:
var text = 'London Bridge is Falling Down FALLING down, fALLiNG Down!';var highlighted = ;console;
The resulting highlighted
London Bridge is Falling DownFALLING down,fALLiNG Down!
Partial match for Keyword in Text
Imagine you are looking for PHP
and someone has
listed phpunit
in their recent experience,
this would be highliged as:
We used phpunit for our TDD.
Match Several Distinct Keywords
If you pass in a String
which contains multiple keywords,
it will be split()
into an Array
and then each
word will be highlighted in the text
:
How?
Install the module from NPM:
npm install keyword-highlighter --save
Use it in your code:
var highlighter = ;var text = 'I write JavaScript Code in Lovely London!';var result = ;console;
This will output:
I write JavaScriptCode in Lovely London!
Done.
Discussion:
Is it Fast?
Try it! And Judge for yourself!
Should we only sub complete words...?
Consider the following example:
var highlighter = ;var text = 'She sells sea shells on the sea shore.';var result = ;console;
This will output:
She sellssea shellson the sea shore.
I'm not convinced its useful to highlight she
in the string shell
... you? Please discuss