The MatchType enum determines the context in which a rule should match words in the input text. This allows you to control whether a pattern matches anywhere, only as a complete word, or only when surrounded by whitespace.
enum MatchType { Alone = 'a', // Surrounded by whitespace Any = '', // Match anywhere (default) Whole = 'w' // Complete word boundaries}
The rule matches only when the word appears as a complete word, not as part of a larger word. Letters and diacritics around the match are disallowed, but punctuation and symbols are allowed.
import { buildTrie, searchAndReplace, MatchType } from 'trie-rules';const rules = [ { from: ['ASWJ'], to: 'Ahl al-Sunnah waʿl-Jamāʿah', options: { match: MatchType.Whole } }];const trie = buildTrie(rules);console.log(searchAndReplace(trie, 'The belief of ASWJ is clear.'));// Output: "The belief of Ahl al-Sunnah waʿl-Jamāʿah is clear."console.log(searchAndReplace(trie, 'ASWJextended'));// Output: "ASWJextended" (no match - part of larger word)
MatchType.Whole uses Unicode-aware letter detection (\p{L}) to determine word boundaries, ensuring proper handling of diacritics and international characters.
The rule matches only when the word is surrounded by whitespace (spaces, tabs, newlines).
import { buildTrie, searchAndReplace, MatchType } from 'trie-rules';const rules = [ { from: ['RH'], to: '(may Allah be pleased with him) reported that the Messenger ﷺ said:', options: { match: MatchType.Alone } }];const trie = buildTrie(rules);console.log(searchAndReplace(trie, 'Abu Hurayrah RH Actions'));// Output: "Abu Hurayrah (may Allah be pleased with him) reported that the Messenger ﷺ said: Actions"console.log(searchAndReplace(trie, 'ARH'));// Output: "ARH" (no match - not surrounded by whitespace)
MatchType.Alone is useful for abbreviations that should only be replaced when they appear as standalone tokens.
Matching is case-insensitive, and the replacement adjusts its initial casing to match the source. If the source starts with a capital letter (ignoring symbols), the replacement will too.
import { buildTrie, searchAndReplace, CaseSensitivity } from 'trie-rules';const rules = [ { from: ['example'], to: 'replacement', options: { casing: CaseSensitivity.Insensitive } }];const trie = buildTrie(rules);console.log(searchAndReplace(trie, 'example text'));// Output: "replacement text"console.log(searchAndReplace(trie, 'Example text'));// Output: "Replacement text" (capitalized to match source)console.log(searchAndReplace(trie, 'EXAMPLE text'));// Output: "REPLACEMENT text" (uppercased to match source)
During buildTrie, case-insensitive rules automatically generate three variants: lowercase, uppercase, and capitalized. This ensures fast lookups without runtime case conversion.
Clipping patterns allow you to remove specific characters immediately before (clipStartPattern) or after (clipEndPattern) a match. This is useful for handling punctuation, apostrophes, or other boundary characters.
Removes matching characters immediately after the matched word.
const rules = [ { from: ['test'], to: 'result', options: { clipEndPattern: /[`'ʾʿ'']+$/ } }];const trie = buildTrie(rules);console.log(searchAndReplace(trie, "The test' worked"));// Output: "The result worked" (apostrophe after 'test' is removed)
Clipping patterns can be either a RegExp or a TriePattern value. Use custom RegExp patterns for advanced clipping logic.
Here’s a comprehensive example combining multiple matching options:
import { buildTrie, searchAndReplace, MatchType, CaseSensitivity, TriePattern} from 'trie-rules';const rules = [ // Abbreviation that must stand alone { from: ['RH'], to: '(may Allah be pleased with him) reported:', options: { match: MatchType.Alone } }, // Acronym that must be a complete word { from: ['ASWJ'], to: 'Ahl al-Sunnah waʿl-Jamāʿah', options: { match: MatchType.Whole } }, // Case-insensitive with apostrophe clipping { from: ['testword'], to: 'replacement', options: { casing: CaseSensitivity.Insensitive, clipStartPattern: TriePattern.Apostrophes, clipEndPattern: /[`'ʾʿ'']+$/ } }];const trie = buildTrie(rules);const text = ` The belief of ASWJ is clear. Abu Hurayrah RH Actions are judged. The 'testword' is here, and Testword' too.`;console.log(searchAndReplace(trie, text));/*Output: The belief of Ahl al-Sunnah waʿl-Jamāʿah is clear. Abu Hurayrah (may Allah be pleased with him) reported: Actions are judged. The replacement is here, and Replacement too.*/