RegExpString.ts overview
Composable regular-expression patterns expressed as plain strings.
Mental model
- Patterns are produced as
string’s and combined asstring’s. The final pattern is fed to theRegExpconstructor 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
- Constructors
- Instances
- CR
- LF
- anyChar
- anyCharButLineBreak
- anyWord
- anyWordLetter
- anythingButDot
- arrowbase
- backslash
- base10Number
- binaryInt
- digit
- dollar
- dot
- hexaInt
- letter
- lineBreak
- lowerCaseLetter
- lowerCaseLetterOrDigit
- minus
- nonSpace
- nonZeroDigit
- octalInt
- plus
- semVer
- sign
- slash
- space
- spaces
- star
- tab
- universalPathSep
- unsignedBase10Int
- unsignedNonNullBase10Int
- upperCaseLetter
- Models
- Utils
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
Constructors
fromRegExp
Returns the source pattern of self as a Type.
Signature
declare const fromRegExp: (self: MRegExp.Type) => Type
Instances
CR
A regular expression string representing a carriage return
Signature
declare const CR: string
LF
A regular expression string representing a line-feed
Signature
declare const LF: string
anyChar
A regular expression string representing any character
Signature
declare const anyChar: string
anyCharButLineBreak
A regular expression string representing any character but a lineBreak
Signature
declare const anyCharButLineBreak: string
anyWord
A regular expression string representing a word
Signature
declare const anyWord: string
anyWordLetter
A regular expression string representing a word letter
Signature
declare const anyWordLetter: string
anythingButDot
A regular expression string representing anything but a dot
Signature
declare const anythingButDot: string
arrowbase
A regular expression string representing the arrowbase
Signature
declare const arrowbase: string
backslash
A regular expression string representing a backslashString
Signature
declare const backslash: string
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 fromfractionalSeparator. 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 fromthousandSeparator. 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’ asfillCharand 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
binaryInt
A regular expression string representing an integer in base 2.
Signature
declare const binaryInt: string
digit
A regular expression string representing a digit
Signature
declare const digit: string
dollar
A regular expression string representing a dollar sign
Signature
declare const dollar: string
dot
A regular expression string representing a dot
Signature
declare const dot: string
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
hexaInt
A regular expression string representing an integer in base 16.
Signature
declare const hexaInt: string
letter
A regular expression string representing a letter
Signature
declare const letter: string
lineBreak
A regular expression string representing a linebreak in Windows, Unix and Mac Os
Signature
declare const lineBreak: string
lowerCaseLetter
A regular expression string representing a lowercase letter
Signature
declare const lowerCaseLetter: string
lowerCaseLetterOrDigit
A regular expression string representing a lowercase letter or a digit.
Signature
declare const lowerCaseLetterOrDigit: string
minus
A regular expression string representing a minus sign
Signature
declare const minus: string
nonSpace
A regular expression string representing a non-space character.
Signature
declare const nonSpace: string
nonZeroDigit
A regular expression string representing a strictly positive digit
Signature
declare const nonZeroDigit: string
octalInt
A regular expression string representing an integer in base 8.
Signature
declare const octalInt: string
plus
A regular expression string representing a plus sign
Signature
declare const plus: string
semVer
A regular expression string representing a simplified SemVer. See https://semver.org/ for a more accurate version
Signature
declare const semVer: string
sign
A regular expression string representing a plus or a minus sign
Signature
declare const sign: string
slash
A regular expression string representing a slash
Signature
declare const slash: string
space
A regular expression string representing a space
Signature
declare const space: string
spaces
A regular expression string representing zero or more spaces
Signature
declare const spaces: string
star
A regular expression string representing a star
Signature
declare const star: string
tab
A regular expression string representing a tab
Signature
declare const tab: string
universalPathSep
A path separator regular expression string to split all possible paths
Signature
declare const universalPathSep: string
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
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
upperCaseLetter
A regular expression string representing an uppercase letter
Signature
declare const upperCaseLetter: string
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
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
atEnd
Anchors self to the end of a line by appending $.
Signature
declare const atEnd: (self: Type) => Type
atStart
Anchors self to the start of a line by prepending ^.
Signature
declare const atStart: (self: Type) => Type
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
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
either
Returns a pattern matching any one of args (alternation).
- Empty strings in
argsare 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
makeLine
Wraps self between ^ and $ so it must span an entire line.
Signature
declare const makeLine: (self: Type) => Type
negativeLookAhead
Wraps self in a negative lookahead (?!…).
Signature
declare const negativeLookAhead: (self: Type) => Type
oneOrMore
Wraps self so it may appear one or more times, using a non-capturing group.
Signature
declare const oneOrMore: (self: Type) => Type
optional
Wraps self so it is optional, using a non-capturing group.
Signature
declare const optional: (self: Type) => Type
optionalCapture
Combines capture and optional: wraps self in a named capture group followed by ?.
Signature
declare const optionalCapture: (name: string) => (self: Type) => Type
positiveLookAhead
Wraps self in a positive lookahead (?=…).
Signature
declare const positiveLookAhead: (self: Type) => Type
repeatBetween
Wraps self so it may appear between low and high times.
lowmust be a non-negative integer withlow <= high.highmust be a strictly positive integer or+Infinity;+Infinityis 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
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