RegExp.ts
RegExp.ts overview
Section titled “RegExp.ts overview”Tools for working with JavaScript regular expressions from the Effect module
namespace. The module exposes the native RegExp constructor, a guard for
narrowing unknown values, and escaping for literal text that will be embedded
in a pattern.
Reach for RegExp when you need to build a regular expression from user or
data-driven text, check whether an unknown value is already a RegExp, or
access the native constructor without leaving the Effect namespace.
Since v2.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”RegExp
Section titled “RegExp”escape
Section titled “escape”Escapes special characters in a regular expression pattern.
When to use
Use to turn literal text into a safe regular expression pattern fragment.
Example (Escaping a pattern string)
import { RegExp } from "effect"import * as assert from "node:assert"
assert.deepStrictEqual(RegExp.escape("a*b"), "a\\*b")Signature
declare const escape: (string: string) => stringSince v2.0.0
constructors
Section titled “constructors”RegExp
Section titled “RegExp”Exposes the JavaScript regular expression constructor from globalThis.
When to use
Use to construct JavaScript regular expressions through the Effect module namespace.
Example (Creating a regular expression)
import { RegExp } from "effect"
// Create a regular expression using Effect's RegExp constructorconst pattern = new RegExp.RegExp("hello", "i")
// Test the patternconsole.log(pattern.test("Hello World")) // trueconsole.log(pattern.test("goodbye")) // falseSignature
declare const RegExp: RegExpConstructorSince v4.0.0
guards
Section titled “guards”isRegExp
Section titled “isRegExp”Checks whether a value is a RegExp.
When to use
Use to validate unknown input before treating it as a regular expression.
Example (Checking for regular expressions)
import { RegExp } from "effect"import * as assert from "node:assert"
assert.deepStrictEqual(RegExp.isRegExp(/a/), true)assert.deepStrictEqual(RegExp.isRegExp("a"), false)Signature
declare const isRegExp: (input: unknown) => input is RegExpSince v3.9.0