Skip to main content Link Search Menu Expand Document (external link)

RegExpString.ts overview

Composable regular-expression patterns expressed as plain strings.

Mental model

  • Patterns are produced as string’s and combined as string’s. The final pattern is fed to the RegExp constructor by callers (or by helpers in | MRegExp).
  • Combinators (zeroOrMore, oneOrMore, either, …) wrap their argument in a non-capturing group (?:…) so they remain composable without affecting capture indices.
  • Pre-built instances cover the common building blocks (digits, sign, separators, line breaks) and a few full patterns (semVer, email, base10Number).

Common tasks

  • Quantifiers: zeroOrMore, oneOrMore, repeatBetween, optional
  • Composition: either, anyCharIn, charNotIn
  • Anchors / lookarounds: makeLine, atStart, atEnd, positiveLookAhead, negativeLookAhead
  • Capturing: capture, optionalCapture
  • Numbers: unsignedBase10Int, unsignedNonNullBase10Int, base10Number, binaryInt, octalInt, hexaInt
  • Pre-built: semVer, email, lineBreak, universalPathSep

Quickstart

Example (Compose a pattern)

import { pipe } from "effect"
import * as MRegExpString from "@parischap/effect-lib/MRegExpString"

const pattern = pipe(MRegExpString.digit, MRegExpString.oneOrMore, MRegExpString.makeLine)
console.log(new RegExp(pattern).test("12345")) // true
console.log(new RegExp(pattern).test("12a45")) // false

Exports Grouped by Category


Constants

DIGIT_GROUP_SIZE

Size of a group of digits separated by a thousand separator (e.g. 1,234,567).

Signature

declare const DIGIT_GROUP_SIZE: 3

Source

Constructors

fromRegExp

Returns the source pattern of self as a Type.

Signature

declare const fromRegExp: (self: MRegExp.Type) => Type

Source

Instances

CR

A regular expression string representing a carriage return

Signature

declare const CR: string

Source

LF

A regular expression string representing a line-feed

Signature

declare const LF: string

Source

anyChar

A regular expression string representing any character

Signature

declare const anyChar: string

Source

anyCharButLineBreak

A regular expression string representing any character but a lineBreak

Signature

declare const anyCharButLineBreak: string

Source

anyWord

A regular expression string representing a word

Signature

declare const anyWord: string

Source

anyWordLetter

A regular expression string representing a word letter

Signature

declare const anyWordLetter: string

Source

anythingButDot

A regular expression string representing anything but a dot

Signature

declare const anythingButDot: string

Source

arrowbase

A regular expression string representing the arrowbase

Signature

declare const arrowbase: string

Source

backslash

A regular expression string representing a backslashString

Signature

declare const backslash: string

Source

base10Number

Returns a regular expression string representing a left-padded number in base 10 using thousandSeparator as thousand separator, fractionalSeparator as fractional separator and eNotationChars as possible characters for scientific notation.

  • thousandSeparator: Usually a string made of at most one character but not mandatory. Should be different from fractionalSeparator. Will not throw otherwise but unexpected results might occur. Use ‘’ for no thousand separator.
  • fractionalSeparator: usually a one-character string but not mandatory (e.g. ‘.’). Should not be an empty string and be different from thousandSeparator. Will not throw otherwise but unexpected results might occur.
  • eNotationChars: array of possible chracters that can be used to represent an exponent (e.g. value: [‘E’, ‘e’]).
  • fillChar: usually a one-character string but not mandatory (e.g. ‘ ‘). If not an empty string, zero or more fillChar’s are tolerated between the sign and the number (or at the start of the number if it is unsigned). Beware if you use a digit as fillChar (e.g. you use ‘0’ as fillChar and try to parse ‘0000’)

Signature

declare const base10Number: ({
  thousandSeparator,
  fractionalSeparator,
  eNotationChars,
  fillChar
}: {
  readonly thousandSeparator: string
  readonly fractionalSeparator: string
  readonly eNotationChars: ReadonlyArray<string>
  readonly fillChar: string
}) => Type

Source

binaryInt

A regular expression string representing an integer in base 2.

Signature

declare const binaryInt: string

Source

digit

A regular expression string representing a digit

Signature

declare const digit: string

Source

dollar

A regular expression string representing a dollar sign

Signature

declare const dollar: string

Source

dot

A regular expression string representing a dot

Signature

declare const dot: string

Source

email

A regular expression string representing an email - Imported from https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression

Signature

declare const email: string

Source

hexaInt

A regular expression string representing an integer in base 16.

Signature

declare const hexaInt: string

Source

letter

A regular expression string representing a letter

Signature

declare const letter: string

Source

lineBreak

A regular expression string representing a linebreak in Windows, Unix and Mac Os

Signature

declare const lineBreak: string

Source

lowerCaseLetter

A regular expression string representing a lowercase letter

Signature

declare const lowerCaseLetter: string

Source

lowerCaseLetterOrDigit

A regular expression string representing a lowercase letter or a digit.

Signature

declare const lowerCaseLetterOrDigit: string

Source

minus

A regular expression string representing a minus sign

Signature

declare const minus: string

Source

nonSpace

A regular expression string representing a non-space character.

Signature

declare const nonSpace: string

Source

nonZeroDigit

A regular expression string representing a strictly positive digit

Signature

declare const nonZeroDigit: string

Source

octalInt

A regular expression string representing an integer in base 8.

Signature

declare const octalInt: string

Source

plus

A regular expression string representing a plus sign

Signature

declare const plus: string

Source

semVer

A regular expression string representing a simplified SemVer. See https://semver.org/ for a more accurate version

Signature

declare const semVer: string

Source

sign

A regular expression string representing a plus or a minus sign

Signature

declare const sign: string

Source

slash

A regular expression string representing a slash

Signature

declare const slash: string

Source

space

A regular expression string representing a space

Signature

declare const space: string

Source

spaces

A regular expression string representing zero or more spaces

Signature

declare const spaces: string

Source

star

A regular expression string representing a star

Signature

declare const star: string

Source

tab

A regular expression string representing a tab

Signature

declare const tab: string

Source

universalPathSep

A path separator regular expression string to split all possible paths

Signature

declare const universalPathSep: string

Source

unsignedBase10Int

Returns a regular expression string representing an unsigned integer in base 10 using thousandSeparator as thousand separator. Pass an empty string for no thousand separator.

Signature

declare const unsignedBase10Int: (thousandSeparator: string) => Type

Source

unsignedNonNullBase10Int

Returns a regular expression string representing an unsigned non-null integer in base 10 using thousandSeparator as thousand separator. Pass an empty string for no thousand separator.

Signature

declare const unsignedNonNullBase10Int: (thousandSeparator: string) => Type

Source

upperCaseLetter

A regular expression string representing an uppercase letter

Signature

declare const upperCaseLetter: string

Source

Models

Type (type alias)

Type on which this module’s functions operate. The string must be a meaningful regular expression string

Signature

type Type = string

Source

Utils

anyCharIn

Returns a pattern matching any one of the supplied characters as a character class.

  • For an empty input returns the empty string.
  • For a single argument returns it verbatim (no class is added).
  • For two or more, builds [abc…].

Example

import * as MRegExpString from "@parischap/effect-lib/MRegExpString"

console.log(MRegExpString.anyCharIn(["a", "b", "c"])) // '[abc]'

Signature

declare const anyCharIn: (args: ReadonlyArray<string>) => Type

Source

atEnd

Anchors self to the end of a line by appending $.

Signature

declare const atEnd: (self: Type) => Type

Source

atStart

Anchors self to the start of a line by prepending ^.

Signature

declare const atStart: (self: Type) => Type

Source

capture

Wraps self in a named capture group (?<name>…).

Example

import { pipe } from "effect"
import * as MRegExpString from "@parischap/effect-lib/MRegExpString"

console.log(pipe(MRegExpString.digit, MRegExpString.capture("d"))) // '(?<d>\\d)'

Signature

declare const capture: (name: string) => (self: Type) => Type

Source

charNotIn

Returns a pattern matching any character not in the supplied set, as a negated character class [^abc…].

Signature

declare const charNotIn: (args: MTypes.ReadonlyOverOne<string>) => Type

Source

either

Returns a pattern matching any one of args (alternation).

  • Empty strings in args are dropped.
  • For a single non-empty argument, returns it verbatim (no group is added).
  • For two or more, joins with | inside a non-capturing group.

Example

import * as MRegExpString from "@parischap/effect-lib/MRegExpString"

console.log(MRegExpString.either("foo", "bar", "baz")) // '(?:foo|bar|baz)'

Signature

declare const either: (...args: ReadonlyArray<Type>) => Type

Source

makeLine

Wraps self between ^ and $ so it must span an entire line.

Signature

declare const makeLine: (self: Type) => Type

Source

negativeLookAhead

Wraps self in a negative lookahead (?!…).

Signature

declare const negativeLookAhead: (self: Type) => Type

Source

oneOrMore

Wraps self so it may appear one or more times, using a non-capturing group.

Signature

declare const oneOrMore: (self: Type) => Type

Source

optional

Wraps self so it is optional, using a non-capturing group.

Signature

declare const optional: (self: Type) => Type

Source

optionalCapture

Combines capture and optional: wraps self in a named capture group followed by ?.

Signature

declare const optionalCapture: (name: string) => (self: Type) => Type

Source

positiveLookAhead

Wraps self in a positive lookahead (?=…).

Signature

declare const positiveLookAhead: (self: Type) => Type

Source

repeatBetween

Wraps self so it may appear between low and high times.

  • low must be a non-negative integer with low <= high.
  • high must be a strictly positive integer or +Infinity; +Infinity is rendered as an open-ended quantifier {low,}.

Example

import { pipe } from "effect"
import * as MRegExpString from "@parischap/effect-lib/MRegExpString"

console.log(pipe("a", MRegExpString.repeatBetween(2, 4))) // '(?:a){2,4}'
console.log(pipe("a", MRegExpString.repeatBetween(2, Infinity))) // '(?:a){2,}'

Signature

declare const repeatBetween: (low: number, high: number) => (self: Type) => Type

Source

zeroOrMore

Wraps self so it may appear zero or more times, using a non-capturing group.

Example

import * as MRegExpString from "@parischap/effect-lib/MRegExpString"

console.log(MRegExpString.zeroOrMore("a")) // '(?:a)*'

Signature

declare const zeroOrMore: (self: Type) => Type

Source