Skip to curriculum
Jump to the editor

23.4. Highlight search matches in text

Medium
Topics
15–30 min

A search box filters a list, and each result shows the matching part in bold or with a yellow background: type "react" and "Learn React today" lights up. It looks like a one-line replace, but it is not. The words may overlap, the query may contain characters such as + or (, and the text comes from users, so the result must be safe HTML.

Write highlight(text, query) in highlight.js. It returns the text as HTML, with every match of the query's words wrapped in <mark> and </mark>.

Signature

JAVASCRIPT
function highlight(text, query) // returns a string of HTML
  • text: the text to show. It is untrusted.
  • query: one or more words separated by whitespace. It is untrusted.

Requirements

  1. Every occurrence of every word of the query is marked, not only the first. Matching ignores case, but the output keeps the text's own letters.
  2. The query is split on whitespace; empty words are ignored. An empty or blank query marks nothing.
  3. Overlapping matches, of one word or of different words, and matches that touch (one ends where the next starts) become a single <mark>. Matches with any character between them stay separate.
  4. The query's characters are plain text: ., *, +, ( and $ match only themselves.
  5. The text is escaped, inside and outside the marks: & to &amp;, < to &lt;, > to &gt;, " to &quot;, ' to &#39;.
  6. Matches are found in the original text, never in the escaped HTML: the query amp does not match inside &amp;, and the query < marks a < character.
  7. Long texts with many matches are handled in well under a second.

Examples

JAVASCRIPT
highlight("Learn React today", "react");
// "Learn <mark>React</mark> today"
JAVASCRIPT
highlight("reaction and act", "react action");
// "<mark>reaction</mark> and <mark>act</mark>": react and action overlap, so they share one mark
JAVASCRIPT
highlight("Use <b> & C++", "c++ <b>");
// "Use <mark>&lt;b&gt;</mark> &amp; <mark>C++</mark>"

highlight("aaaa", "aa");
// "<mark>aaaa</mark>"

Environment

  • Plain JavaScript in a browser page; the tests call the global highlight from highlight.js.
  • Case is compared with toLowerCase(). Letters whose lower case has a different length (such as the Turkish dotted capital I) are out of scope, as are whole-word matching and accents.
  • Any built-in is allowed.

Hints

  1. Can you first collect the matches as [start, end] positions, and only then build the HTML?
  2. Once positions are sorted by start, how do you tell that the next one overlaps or touches the last one you kept?
  3. Why does escaping first, then searching, give wrong results?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…