Highlight Words Core

1.2.3 · maintenance · verified Sun Apr 19

highlight-words-core is a foundational utility library that provides core logic for identifying and segmenting matching words within a larger text string. It serves as the headless engine for UI components like `react-highlight-words` and `react-native-highlight-words`, abstracting away the text processing logic. The current stable version is 1.2.3. As a low-level utility, its release cadence is infrequent, primarily receiving updates for critical bug fixes or performance enhancements rather than new features. Its key differentiator is its framework-agnostic nature, allowing developers to implement custom rendering logic on top of its chunking output, making it versatile for various highlighting applications beyond specific UI frameworks.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `findAll` to process text and produce HTML with highlighted matches, including options for `autoEscape` and `caseSensitive`.

import { findAll } from 'highlight-words-core';

const textToHighlight = 'This is some text to highlight.';
const searchWords = ['This', 'i', 'highlight.'];

// Find all occurrences of searchWords in textToHighlight
const chunks = findAll({
  searchWords,
  textToHighlight,
  autoEscape: true, // Recommended for user-provided search terms
  caseSensitive: false // Set to true for exact case matches
});

// Example of how to render the highlighted text in a web context
const highlightedHtml = chunks
  .map(chunk => {
    const { end, highlight, start } = chunk;
    const text = textToHighlight.substring(start, end);
    if (highlight) {
      return `<mark style="background-color: yellow;">${text}</mark>`;
    } else {
      return text;
    }
  })
  .join('');

console.log(highlightedHtml); // Outputs: <mark style="background-color: yellow;">This</mark> is some text to <mark style="background-color: yellow;">highlight.</mark>

view raw JSON →