Skip to main content

@rregex/rregex@1.10.11

A dependency-free WebAssembly build of Rust Regex for Javascript

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
It is unknown whether this package works with Browsers
JSR Score
76%
Published
3 weeks ago
indexRRegexSet
class RRegexSet
deno add @rregex/rregex
import { RRegexSet } from "@rregex/rregex";
npx jsr add @rregex/rregex
import { RRegexSet } from "@rregex/rregex";
yarn dlx jsr add @rregex/rregex
import { RRegexSet } from "@rregex/rregex";
pnpm dlx jsr add @rregex/rregex
import { RRegexSet } from "@rregex/rregex";
bunx jsr add @rregex/rregex
import { RRegexSet } from "@rregex/rregex";

Match multiple (possibly overlapping) regular expressions in a single scan. A regex set corresponds to the union of two or more regular expressions.

That is, a regex set will match text where at least one of its constituent regular expressions matches. A regex set as its formulated here provides a touch more power: it will also report which regular expressions in the set match. Indeed, this is the key difference between regex sets and a single Regex with many alternates, since only one alternate can match at a time.

Constructors

new
RRegexSet(list)

Create a new regex set with the given regular expressions.

This takes an of strings, if any item in the list is not a valid regular expressions, then an error is returned.

Example

Create a new regex set from an iterator of strings:

import { RegexSet } from "rregex"

const set = new RegexSet(["\w+", "\d+"])
expect(set.is_match("foo")).toBe(true)

Methods

free(): void
isMatch(text)

Returns true if and only if one of the regexes in this set matches the text given.

This method should be preferred if you only need to test whether any of the regexes in the set should match, but don't care about which regexes matched. This is because the underlying matching engine will quit immediately after seeing the first match instead of continuing to find all matches.

Note that as with searches using Regex, the expression is unanchored by default. That is, if the regex does not start with ^ or \A, or end with $ or \z, then it is permitted to match anywhere in the text.

Example

Tests whether a set matches some text:

import { RegexSet } from "rregex"

const set = new RegexSet(["\w+", "\d+"])
expect(set.is_match("foo")).toBe(true)
expect(!set.is_match("☃")).toBe(false)
matches(text)

Returns the set of regular expressions that match in the given text.

The set returned contains the index of each regular expression that matches in the given text. The index is in correspondence with the order of regular expressions given to RegexSet's constructor.

The set can also be used to iterate over the matched indices.

Note that as with searches using Regex, the expression is unanchored by default. That is, if the regex does not start with ^ or \A, or end with $ or \z, then it is permitted to match anywhere in the text.

Example

Tests which regular expressions match the given text:

import { RegexSet } from "rregex"

const set = new RegexSet([
    "\w+",
    "\d+",
    "\pL+",
    "foo",
    "bar",
    "barfoo",
    "foobar",
])
const matches = set.matches("foobar")
expect(matches).toBe([0, 2, 3, 4, 6])