Highlight matching text in JavaScript
In the previous post about search with typo tolerance, I added a few interactive elements to demonstrate the idea of how we can improve search functionality on the page by being more tolerant to typos. You might be curious how I made highlighting of matching text within results. So here it is.
It's not super complicated but I'll give you a very nice hint you might not know :) Here is the demo. Use Search input to search trough given text:
The trick is to replace all occurrences of searched text with the same text but wrapped with a <mark>
this time.
We will also add a highlight
CSS class to that <mark>
so we will be able to style it accordingly.
You don't need any JS library for that. Here is the code that does the job:
Available on1const $box = document.getElementById('box');2const $search = document.getElementById('search');34$search.addEventListener('input', (event) => {5 const searchText = event.target.value;6 const regex = new RegExp(searchText, 'gi');78 let text = $box.innerHTML;9 text = text.replace(/(<mark class="highlight">|<\/mark>)/gim, '');1011 const newText = text.replace(regex, '<mark class="highlight">$&</mark>');12 $box.innerHTML = newText;13});14
Let's assume the $box
is the element that contains text (it could be a whole page) and the $search
is the input.
In line 8 we get the current HTML in the $box
and remove all current highlights
in the following line. We do that to clean-up after ourselves. We don't want to keep old searches (or partial searches) on the screen.
You can play with that on codepen so you'll see the HTML structure and
CSS styles (where only the .highlight is important).
The hint I've mentioned before you could potentially miss is $&
in the second argument of the replace
method. This is
a special replacement pattern that tells the replacer method to insert the matched substring there.
Why we won't simply use something like this? So inserting the searched text?
// ...const searchText = event.target.value;// ...const newText = text.replace(regex,`<mark class="highlight">${searchText}</mark>`);
By doing that we will get into trouble with the case of the letters. Most search/find functionality is
case insensitive so we don't want to mess with that. Consider the example below, where I simply wrap the searched text
with a <mark>
with that text inside:
It's strange, isn't it? Fortunately, we don't have to be super clever to keep the case of the matched text. We just need
to use $&
with the replace
method.
React implementation
React seems to be the most popular framework library that people use these days. But no matter what front-end
framework you use, you'll probably pass text
as an argument to a component with search-and-highlight functionality.
It could be also a label of searchable items on a list.
That simplifies things a bit because we don't have to get a raw text from DOM elements. And we don't have to clean up after ourselves. We can focus on the wrapping part and leave the rendering to the rendering engine:
Available on1import React, { Component } from 'react';23export default class HighlightText extends Component {4 constructor(props) {5 super(props);6 this.state = { searchText: '' };7 this.search = this.search.bind(this);8 }910 search(event) {11 this.setState({ searchText: event.target.value });12 }1314 _getText(text, searchText) {15 return searchText ? this._getTextWithHighlights(text, searchText) : text;16 }1718 _getTextWithHighlights(text, searchText) {19 const regex = new RegExp(searchText, 'gi');20 const newText = text.replace(regex, `<mark class="highlight">$&</mark>`);21 return <span dangerouslySetInnerHTML={{ __html: newText }} />;22 }2324 render() {25 const { cite, text } = this.props;26 const { searchText } = this.state;27 const textToShow = this._getText(text, searchText);2829 return (30 <div className="container">31 <div className="search-container">32 <label htmlFor="search">Search within quoted text</label>33 <input34 id="search"35 placeholder="Type `web` for example"36 type="search"37 autoComplete="off"38 onChange={this.search}39 value={searchText}40 />41 </div>42 <blockquote cite={cite}>{textToShow}</blockquote>43 </div>44 );45 }46}47
The most important lines in this implementation are lines 20 and 21. The first one is the heart of highlighting implementation and the second makes sure to set dangerous HTML content within an element.
What's so dangerous about the wrapped searched text?
Every framework has to sanitize raw HTML if you plan to display it on the screen. Here we are sure that the content is ok. It's provided by the user but not displayed anywhere else than their computer so it's safe by definition.
Search for "html safe + framework name" to find a way to force the rendering engine to display a wrapped element.
Good luck!
If you'd like to see more content like this, please follow me on Twitter: tomekdev_. I usually write about interfaces in the real life. So where the design meets implementation.
Comments and discussion welcomed in this thread.