{"id":11039,"library":"highlight-words-core","title":"Highlight Words Core","description":"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.","status":"maintenance","version":"1.2.3","language":"javascript","source_language":"en","source_url":"github.com/bvaughn/highlight-words-core","tags":["javascript","highlighter","highlight","text","words","matches","substring","occurrences","search"],"install":[{"cmd":"npm install highlight-words-core","lang":"bash","label":"npm"},{"cmd":"yarn add highlight-words-core","lang":"bash","label":"yarn"},{"cmd":"pnpm add highlight-words-core","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const findAll = require('highlight-words-core');","symbol":"findAll","correct":"import { findAll } from 'highlight-words-core';"}],"quickstart":{"code":"import { findAll } from 'highlight-words-core';\n\nconst textToHighlight = 'This is some text to highlight.';\nconst searchWords = ['This', 'i', 'highlight.'];\n\n// Find all occurrences of searchWords in textToHighlight\nconst chunks = findAll({\n  searchWords,\n  textToHighlight,\n  autoEscape: true, // Recommended for user-provided search terms\n  caseSensitive: false // Set to true for exact case matches\n});\n\n// Example of how to render the highlighted text in a web context\nconst highlightedHtml = chunks\n  .map(chunk => {\n    const { end, highlight, start } = chunk;\n    const text = textToHighlight.substring(start, end);\n    if (highlight) {\n      return `<mark style=\"background-color: yellow;\">${text}</mark>`;\n    } else {\n      return text;\n    }\n  })\n  .join('');\n\nconsole.log(highlightedHtml); // Outputs: <mark style=\"background-color: yellow;\">This</mark> is some text to <mark style=\"background-color: yellow;\">highlight.</mark>","lang":"javascript","description":"Demonstrates how to use `findAll` to process text and produce HTML with highlighted matches, including options for `autoEscape` and `caseSensitive`."},"warnings":[{"fix":"Pass `autoEscape: true` to the `findAll` function options: `findAll({ searchWords, textToHighlight, autoEscape: true })`.","message":"When `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Set `caseSensitive: true` in the `findAll` function options: `findAll({ searchWords, textToHighlight, caseSensitive: true })`.","message":"The `caseSensitive` option defaults to `false`, meaning searches are case-insensitive. If exact case matching is required, this option must be explicitly set to `true`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Install TypeScript types: `npm install --save-dev @types/highlight-words-core` in addition to `npm install highlight-words-core`.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure `autoEscape: true` is passed to `findAll` when `searchWords` are dynamically generated or come from user input.","cause":"A special regular expression character in `searchWords` was not escaped, leading to an invalid regex pattern at runtime.","error":"Uncaught SyntaxError: Invalid regular expression: /[+]/:"},{"fix":"Verify that `searchWords` contains the correct terms. If exact case matches are needed, set `caseSensitive: true` in the `findAll` options.","cause":"This is often due to a mismatch in case sensitivity or incorrect `searchWords` values. By default, searches are case-insensitive.","error":"Highlights are not appearing for expected matches."}],"ecosystem":"npm"}