---
title: "splitHighlight"
description: "Split text into matched and unmatched segments by a case-insensitive keyword, preserving original casing for rendering search highlights."
canonical_url: "https://core.mhaibaraai.cn/en/docs/transformers/string/split-highlight"
---
# splitHighlight

> 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.

```ts
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:The segment text, preserving the original casing.Whether this segment matches the keyword.

## Changelog

See commit history for [src/transformers/string/splitHighlight.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/transformers/string/splitHighlight.ts).

---

- [GitHub](https://github.com/mhaibaraai/movk-core/blob/main/src/transformers/string/splitHighlight.ts)


## Sitemap

See the full [sitemap](https://core.mhaibaraai.cn/sitemap.md) for all pages.
