Skip to content

Aho–Corasick / exact text matching

One dictionary.
One scan.
Every match.

Match a dictionary against text with grapheme precision and slice-ready ranges. Follow every transition, fallback, and suffix match.

he · she · his · hers
ushers → she, he
ushers → hers
search('ushers')
{ pattern: 'she', start: 1, end: 4 }
text.slice(1, 4) === 'she'

Documentation for v3.2.0, available on npm. MIT · SonOfMagic · BrunoRB/ahocorasick

3.2.0current release
grapheme-firstmatching unit
UTF-16slice-ready ranges
0runtime dependencies

Start with a task

A focused API for each kind of text work.

Pick the result you need, then keep the same matcher as your application grows.

The contract to remember

Match the original text. Keep the original coordinates.

Every result uses a half-open UTF-16 range, so the matched text is always text.slice(start, end). Emoji, combining marks, and CRLF stay together as graphemes.

Read the Unicode and index guide →
const text = '😀 café'
const hit = new AhoCorasick(['café']).search(text)[0]
text.slice(hit.start, hit.end)
// 'café'

Keep exploring

From the first match to a production pipeline.

MIT · Based on BrunoRB/ahocorasick