Highlight text in JavaScript

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:

Alongside HTML and CSS, JavaScript is one of the core technologies of the World Wide Web. JavaScript enables interactive web pages and is an essential part of web applications. The vast majority of websites use it for client-side page behavior, and all major web browsers have a dedicated JavaScript engine to execute it.
~ from Wikipedia

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:

1const $box = document.getElementById('box');
2const $search = document.getElementById('search');
3
4$search.addEventListener('input', (event) => {
5 const searchText = event.target.value;
6 const regex = new RegExp(searchText, 'gi');
7
8 let text = $box.innerHTML;
9 text = text.replace(/(<mark class="highlight">|<\/mark>)/gim, '');
10
11 const newText = text.replace(regex, '<mark class="highlight">$&</mark>');
12 $box.innerHTML = newText;
13});
14
Available onAvailable on Codepen

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:

JAVASCRIPT ENABLES INTERACTIVE WEB PAGES AND IS AN ESSENTIAL PART OF WEB APPLICATIONS
~ from Wikipedia

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:

1import React, { Component } from 'react';
2
3export default class HighlightText extends Component {
4 constructor(props) {
5 super(props);
6 this.state = { searchText: '' };
7 this.search = this.search.bind(this);
8 }
9
10 search(event) {
11 this.setState({ searchText: event.target.value });
12 }
13
14 _getText(text, searchText) {
15 return searchText ? this._getTextWithHighlights(text, searchText) : text;
16 }
17
18 _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 }
23
24 render() {
25 const { cite, text } = this.props;
26 const { searchText } = this.state;
27 const textToShow = this._getText(text, searchText);
28
29 return (
30 <div className="container">
31 <div className="search-container">
32 <label htmlFor="search">Search within quoted text</label>
33 <input
34 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
Available onAvailable on CodeSandbox

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!