splitHighlight v1.3.1

Split text into matched and unmatched segments by a case-insensitive keyword, preserving original casing for rendering search highlights.

Usage

splitHighlight splits text into matched (match: true) and unmatched (match: false) segments by a keyword. Concatenating the segments in order reconstructs the original text, making it easy to render search highlights in a UI.

Matching is case-insensitive and matched segments preserve the original casing. When the keyword is empty or the text does not contain the keyword, a single unmatched segment is returned.

import { splitHighlight } from '@movk/core'

splitHighlight('ABC', 'b')
// => [
//   { text: 'A', match: false },
//   { text: 'B', match: true },
//   { text: 'C', match: false }
// ]

splitHighlight('Hello World', '')
// => [{ text: 'Hello World', match: false }]

API

splitHighlight(text: string, term: string): HighlightSegment[]

Parameters

text
string required
The source text.
term
string required
The keyword. Returns a single unmatched segment when the keyword is an empty string.

Returns

HighlightSegment[]
An array of segments that reconstruct the original text when concatenated. Each segment contains:

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License