Registry / highlight-words-core

highlight-words-core

JSON →
library1.2.3jsnpmunverified

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.

npm install highlight-words-core
INSTALL
IMPORT
SIG · HIGHLIGHT-WORDS-CO
H
highlight-words-core
javascriptv1.2.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

findAll
import { findAll } from 'highlight-words-core';
const findAll = require('highlight-words-core');
The package primarily uses named exports and is intended for modern JavaScript environments. While CommonJS might work via transpilation, direct CommonJS require statements are not idiomatic.

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>
Debug
Known issues
gotchaWhen `searchWords` are user-supplied, special regular expression characters (e.g., '.', '*', '+', '?', '(', ')', '[', ']', '{', '}', '\', '|', '^', '$') can cause `findAll` to throw a `SyntaxError: Invalid regular expression` if not properly escaped. Always set `autoEscape: true` for dynamic search terms.
fix
Pass `autoEscape: true` to the `findAll` function options: `findAll({ searchWords, textToHighlight, autoEscape: true })`.
affects: >=1.0.0
gotchaThe `caseSensitive` option defaults to `false`, meaning searches are case-insensitive. If exact case matching is required, this option must be explicitly set to `true`.
fix
Set `caseSensitive: true` in the `findAll` function options: `findAll({ searchWords, textToHighlight, caseSensitive: true })`.
affects: >=1.0.0
gotchaThe package currently ships with JavaScript files, but type declarations are provided separately by `@types/highlight-words-core`. Developers using TypeScript need to install both packages to get type support.
fix
Install TypeScript types: `npm install --save-dev @types/highlight-words-core` in addition to `npm install highlight-words-core`.
affects: >=1.0.0
Errors
Common errors & fixes
Uncaught SyntaxError: Invalid regular expression: /[+]/:
A special regular expression character in `searchWords` was not escaped, leading to an invalid regex pattern at runtime.
fix
Ensure `autoEscape: true` is passed to `findAll` when `searchWords` are dynamically generated or come from user input.
Highlights are not appearing for expected matches.
This is often due to a mismatch in case sensitivity or incorrect `searchWords` values. By default, searches are case-insensitive.
fix
Verify that `searchWords` contains the correct terms. If exact case matches are needed, set `caseSensitive: true` in the `findAll` options.
Upgrade
Version history
1.2.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
highlight-words-core — npm install highlight-words-core · libregistry