Without any match type specified, patterns match anywhere in the text:
import { buildTrie, searchAndReplace } from 'trie-rules';const rules = [ { from: ['Ali'], to: 'ʿAlī', },];const trie = buildTrie(rules);console.log(searchAndReplace(trie, 'Ali went with Alison.'));// Output: 'ʿAlī went with ʿAlīson.'
Without match constraints, patterns may match inside larger words, which is often undesirable.
Use MatchType.Whole to ensure patterns only match complete words:
import { buildTrie, searchAndReplace, MatchType } from 'trie-rules';const rules = [ { from: ['Ali'], to: 'ʿAlī', options: { match: MatchType.Whole, }, },];const trie = buildTrie(rules);console.log(searchAndReplace(trie, 'Ali went with Alison.'));// Output: 'ʿAlī went with Alison.'
MatchType.Whole ensures the match is not part of a larger word. Letters and diacritics around it are disallowed, but punctuation and symbols are allowed.
Use MatchType.Alone for strict matching where the pattern must be surrounded by whitespace:
import { buildTrie, searchAndReplace, MatchType } from 'trie-rules';const rules = [ { from: ['az', 'ath', 'al'], to: 'al-', options: { match: MatchType.Alone, }, },];const trie = buildTrie(rules);console.log(searchAndReplace(trie, 'az-Zuhri'));// Output: 'az-Zuhri' (no match because 'az' is followed by '-')console.log(searchAndReplace(trie, 'az Zuhri'));// Output: 'al-Zuhri' (matches because 'az' is followed by space)
MatchType.Alone is useful for matching standalone words like articles or prepositions that should only be replaced when they appear in isolation.
By default, matching is case-sensitive and the replacement preserves its original casing:
import { buildTrie, searchAndReplace } from 'trie-rules';const rules = [ { from: ['sufyan'], to: 'Sufyān', },];const trie = buildTrie(rules);console.log(searchAndReplace(trie, 'sufyan went home.'));// Output: 'Sufyān went home.'console.log(searchAndReplace(trie, 'Sufyan went home.'));// Output: 'Sufyan went home.' (no match)