Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Irregular expressions. Simplify implementation of classes that fulfil a regex-compatible contract.
Irregex
Irregular expressions. Simplify implementation of classes that fulfil a regex-compatible contract.
Irregex
class
Abstract class for regex-compatible classes to inherit from.
At minimum, derived classes must implement the getMatch
method, which is stateful and relies on the lastIndex
property.
For convenience, you can also use the fromIter
helper, which caches iterable results for a given input string and handles reading of lastIndex
.
Examples
Matching words in a string:
import { Irregex } from 'irregex' class WordMatcher extends Irregex { segmenter: Intl.Segmenter constructor(locale: string) { super() this.segmenter = new Intl.Segmenter(locale, { granularity: 'word' }) } getMatch(str: string) { return this.fromIter(str, function* () { for (const segmentData of this.segmenter.segment(str)) { if (segmentData.isWordLike && /\p{L}/u.test(segmentData.segment)) { const arr: [string] = [segmentData.segment] yield Object.assign(arr, { ...segmentData, groups: { abbr: segmentData.segment.match(/^.{0,3}/u)![0], }, }) } } }) } } new WordMatcher('en-US')[Symbol.replace]( 'Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday', '($<abbr>)', ) // "(Mon), (Tue), (Wed), (Thu), (Fri), (Sat), (Sun)" new WordMatcher('zh-CN')[Symbol.match]('此地无银三百两') // ["此地", "无", "银", "三百", "两"]
Matching IPv4 addresses:
class Ipv4Matcher extends Irregex { re = /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/g constructor() { super() this.trackLastIndex = [this.re] } getMatch(str: string) { while (true) { const m = this.re.exec(str) if (m == null) return null if (m.slice(1).every((x) => Number(x) < 256)) { return m } } } } new Ipv4Matcher()[Symbol.match]('192.168.1.1\n999.999.999.999\n255.255.255.255') // ["192.168.1.1", "255.255.255.255"]
CombinedMatcher
class
Combine multiple matchers (RegExp
s, Irregex
es) to iterate through them in sync.
Examples
Combining different types of matchers:
import { CombinedMatcher } from 'irregex' const matcher = new CombinedMatcher([ new WordMatcher('en-US'), new Ipv4Matcher(), ]) matcher[Symbol.match]('One two three 1 2 3 192.168.1.1 999.999.999.999 255.255.255.255 five!') // ["One", "two", "three", "192.168.1.1", "255.255.255.255", "five"]
Combining case-sensitive and case-insensitive regexes:
import { CombinedMatcher } from 'irregex' const matcher = new CombinedMatcher([ /a/g, /b/gi, ]) matcher[Symbol.match]('a A b B') // ["a", "b", "B"]
Add Package
deno add jsr:@li/irregex
Import symbol
import * as mod from "@li/irregex";
---- OR ----
Import directly with a jsr specifier
import * as mod from "jsr:@li/irregex";
Add Package
npx jsr add @li/irregex
Import symbol
import * as mod from "@li/irregex";
Add Package
yarn dlx jsr add @li/irregex
Import symbol
import * as mod from "@li/irregex";
Add Package
pnpm dlx jsr add @li/irregex
Import symbol
import * as mod from "@li/irregex";
Add Package
bunx jsr add @li/irregex
Import symbol
import * as mod from "@li/irregex";