Functional Expression Reference
🧙 Functional Regular expression builder.
Usage
📦 Node
Install functional-expression
as a dependency:
1pnpm add functional-expression2# or3npm install functional-expression4# or5yarn add functional-expression
Import it and use it:
1import { build, group, or } from "functional-expression";2
3build("gu")(group(or("this", "that"))); // /(?:this|that)/gu
🦕 Deno
Import functional-expression
using the npm:
prefix, and use it directly:
1import { build, group, or } from "npm:functional-expression";2
3build("gu")(group(or("this", "that"))); // /(?:this|that)/gu
🌎 Browser
Import functional-expression
using esm.sh, and use it directly:
1<script type="module">2 import { build, group, or } from "https://esm.sh/functional-expression";3
4 build("gu")(group(or("this", "that"))); // /(?:this|that)/gu5</script>
Useful links
- 📝 Documentation: TypeDoc generated documentation.
- ⏳ Changelog: List of changes between versions.
- ✅ Tests Coverage: Coveralls page with tests coverage.
Anchors
END
1const END: "$" = "$";
Matches the end of the string, or the end of a line if the multiline flag (m) is enabled. This matches a position, not a character.
NOT_WORD_BOUNDARY
1const NOT_WORD_BOUNDARY: "\B";
Matches any position that is not a word boundary. This matches a position, not a character.
START
1const START: "^" = "^";
Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled. This matches a position, not a character.
WORD_BOUNDARY
1const WORD_BOUNDARY: "\b";
Matches a word boundary position between a word character and non-word character or position (start / end of string). See the word character class (w) for more info.
Character classes
ALL
1const ALL: "." = ".";
Matches any character except linebreaks. Equivalent to [^\n\r].
DIGIT
1const DIGIT: "\d";
Matches any digit character (0-9). Equivalent to [0-9].
NOT_DIGIT
1const NOT_DIGIT: "\D";
Matches any character that is not a digit character (0-9). Equivalent to [^0-9].
NOT_WHITESPACE
1const NOT_WHITESPACE: "\S";
Matches any character that is not a whitespace character (spaces, tabs, line breaks).
NOT_WORD
1const NOT_WORD: "\W";
Matches any character that is not a word character (alphanumeric & underscore). Equivalent to [^A-Za-z0-9_]
WHITESPACE
1const WHITESPACE: "\s";
Matches any whitespace character (spaces, tabs, line breaks).
WORD
1const WORD: "\w";
Matches any word character (alphanumeric & underscore). Only matches low-ascii characters (no accented or non-roman characters). Equivalent to [A-Za-z0-9_]
notSet()
1function notSet<Tokens>(2 ...tokens: Tokens3): `[${StringJoin<["^", ...Tokens[]], "", "^", [0]>}]`;
Match any character that is not in the set.
Type parameters
Type parameter |
---|
Tokens extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…tokens | Tokens |
Returns
`[${StringJoin<[”^”, …Tokens[]], "", ”^”, [0]>}]`
notUnicodeCharacterClassEscape()
1function notUnicodeCharacterClassEscape<Category>(category: Category): P{${Category}}
Matches any character that is not in the specified unicode category.
Requires the u flag.
Type parameters
Type parameter |
---|
Category extends Strigifiable |
Parameters
Parameter | Type |
---|---|
category | Category |
Returns
\P{${Category}}
See
Unicode Character class escape
range()
1function range<From>(from: From): <To>(to: To) => `${From}-${To}`;
Matches a character having a character code between the two specified characters
inclusive. Must be used with set
.
Type parameters
Type parameter |
---|
From extends Strigifiable |
Parameters
Parameter | Type |
---|---|
from | From |
Returns
Function
Type parameters
Type parameter |
---|
To extends Strigifiable |
Parameters
Parameter | Type |
---|---|
to | To |
Returns
`${From}-${To}`
set()
1function set<Tokens>(2 ...tokens: Tokens3): `[${StringJoin<[...Tokens[]], "", `${[...Tokens[]][0]}`, [0]>}]`;
Match any character in the set.
Type parameters
Type parameter |
---|
Tokens extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…tokens | Tokens |
Returns
`[${StringJoin<[…Tokens[]], "", `${[…Tokens[]][0]}`, [0]>}]`
unicodeCharacterClassEscape()
1function unicodeCharacterClassEscape<Category>(category: Category): p{${Category}}
Matches a character in the specified unicode category.
Requires the u flag.
Type parameters
Type parameter |
---|
Category extends Strigifiable |
Parameters
Parameter | Type |
---|---|
category | Category |
Returns
\p{${Category}}
See
Unicode Character class escape
Escaped Characters
HexadecimalDigit
1type HexadecimalDigit: `${Digit | "A" | "B" | "C" | "D" | "E" | "F"}`;
Possible digits for an hexadecimal number.
UppercaseLetters
1type UppercaseLetters:2 | "A"3 | "B"4 | "C"5 | "D"6 | "E"7 | "F"8 | "G"9 | "H"10 | "I"11 | "J"12 | "K"13 | "L"14 | "M"15 | "N"16 | "O"17 | "P"18 | "Q"19 | "R"20 | "S"21 | "T"22 | "U"23 | "V"24 | "W"25 | "X"26 | "Y"27 | "Z";
Uppercase letter values.
CARRIAGE_RETURN
1const CARRIAGE_RETURN: "\r";
Matches a CARRIAGE RETURN character (char code 13).
FORM_FEED
1const FORM_FEED: "\f";
Matches a FORM FEED character (char code 12).
LINE_FEED
1const LINE_FEED: "\n";
Matches a LINE FEED character (char code 10).
NULL
1const NULL: "\0";
Matches a NULL character (char code 0).
TAB
1const TAB: "\t";
Matches a TAB character (char code 9).
VERTICAL_TAB
1const VERTICAL_TAB: "\v";
Matches a VERTICAL TAB character (char code 11).
controlCharacter()
1function controlCharacter<Character>(character: Character): c$ {2 Character;3}
Escaped control character in the form \cZ. This can range from \cA (SOH, char code 1) to \cZ (SUB, char code 26).
Type parameters
Type parameter |
---|
Character extends UppercaseLetters |
Parameters
Parameter | Type |
---|---|
character | Character |
Returns
\c${Character}
hexadecimal()
1function hexadecimal<HexadecimalValue>(hexadecimalValue: HexadecimalValue): x$ {2 HexadecimalValue;3}
Hexadecimal escaped character in the form \xFF.
Type parameters
Type parameter |
---|
| HexadecimalValue
extends | "AA"
| "AB"
| "AC"
| "AD"
| "AE"
| "AF"
| "A0"
| "A9"
| "A1"
| "A2"
| "A3"
| "A4"
| "A5"
| "A6"
| "A7"
| "A8"
| "BA"
| "BB"
| "BC"
| "BD"
| "BE"
| "BF"
| "B0"
| "B9"
| "B1"
| "B2"
| "B3"
| "B4"
| "B5"
| "B6"
| "B7"
| "B8"
| "CA"
| "CB"
| "CC"
| "CD"
| "CE"
| "CF"
| "C0"
| "C9"
| "C1"
| "C2"
| "C3"
| "C4"
| "C5"
| "C6"
| "C7"
| "C8"
| "DA"
| "DB"
| "DC"
| "DD"
| "DE"
| "DF"
| "D0"
| "D9"
| "D1"
| "D2"
| "D3"
| "D4"
| "D5"
| "D6"
| "D7"
| "D8"
| "EA"
| "EB"
| "EC"
| "ED"
| "EE"
| "EF"
| "E0"
| "E9"
| "E1"
| "E2"
| "E3"
| "E4"
| "E5"
| "E6"
| "E7"
| "E8"
| "FA"
| "FB"
| "FC"
| "FD"
| "FE"
| "FF"
| "F0"
| "F9"
| "F1"
| "F2"
| "F3"
| "F4"
| "F5"
| "F6"
| "F7"
| "F8"
| "0A"
| "0B"
| "0C"
| "0D"
| "0E"
| "0F"
| "00"
| "09"
| "01"
| "02"
| "03"
| "04"
| "05"
| "06"
| "07"
| "08"
| "9A"
| "9B"
| "9C"
| "9D"
| "9E"
| "9F"
| "90"
| "99"
| "91"
| "92"
| "93"
| "94"
| "95"
| "96"
| "97"
| "98"
| "1A"
| "1B"
| "1C"
| "1D"
| "1E"
| "1F"
| "10"
| "19"
| "11"
| "12"
| "13"
| "14"
| "15"
| "16"
| "17"
| "18"
| "2A"
| "2B"
| "2C"
| "2D"
| "2E"
| "2F"
| "20"
| "29"
| "21"
| "22"
| "23"
| "24"
| "25"
| "26"
| "27"
| "28"
| "3A"
| "3B"
| "3C"
| "3D"
| "3E"
| "3F"
| "30"
| "39"
| "31"
| "32"
| "33"
| "34"
| "35"
| "36"
| "37"
| "38"
| "4A"
| "4B"
| "4C"
| "4D"
| "4E"
| "4F"
| "40"
| "49"
| "41"
| "42"
| "43"
| "44"
| "45"
| "46"
| "47"
| "48"
| "5A"
| "5B"
| "5C"
| "5D"
| "5E"
| "5F"
| "50"
| "59"
| "51"
| "52"
| "53"
| "54"
| "55"
| "56"
| "57"
| "58"
| "6A"
| "6B"
| "6C"
| "6D"
| "6E"
| "6F"
| "60"
| "69"
| "61"
| "62"
| "63"
| "64"
| "65"
| "66"
| "67"
| "68"
| "7A"
| "7B"
| "7C"
| "7D"
| "7E"
| "7F"
| "70"
| "79"
| "71"
| "72"
| "73"
| "74"
| "75"
| "76"
| "77"
| "78"
| "8A"
| "8B"
| "8C"
| "8D"
| "8E"
| "8F"
| "80"
| "89"
| "81"
| "82"
| "83"
| "84"
| "85"
| "86"
| "87"
| "88"
|
Parameters
Parameter | Type |
---|---|
hexadecimalValue | HexadecimalValue |
Returns
\x${HexadecimalValue}
unicode()
1function unicode<HexadecimalValue>(hexadecimalValue: HexadecimalValue): u{${HexadecimalValue}}
Unicode escaped character in the form \u{FFFF}. Supports a full range of unicode point escapes with any number of hex digits.
Requires the unicode flag (u).
Type parameters
Type parameter |
---|
HexadecimalValue extends Strigifiable |
Parameters
Parameter | Type |
---|---|
hexadecimalValue | HexadecimalValue |
Returns
\u{${HexadecimalValue}}
Groups & References
capture()
1function capture<Captured>(2 ...captured: Captured3): `(${StringJoin<[...Captured[]], "", `${[...Captured[]][0]}`, [0]>})`;
Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.
Type parameters
Type parameter |
---|
Captured extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…captured | Captured |
Returns
`(${StringJoin<[…Captured[]], "", `${[…Captured[]][0]}`, [0]>})`
captureNamed()
1function captureNamed<Name>(2 name: Name,3): <Captured>(4 ...captured: Captured5) => `(?<${Name}>${StringJoin<[...Captured[]], "", `${[...Captured[]][0]}`, [0]>})`;
Creates a capturing group that can be referenced via the specified name.
Type parameters
Type parameter |
---|
Name extends Strigifiable |
Parameters
Parameter | Type |
---|---|
name | Name |
Returns
Function
Type parameters
Type parameter |
---|
Captured extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…captured | Captured |
Returns
`(?<${Name}>${StringJoin<[…Captured[]], "", `${[…Captured[]][0]}`, [0]>})`
captureType()
1function captureType<Type>(2 type: Type,3): <Captured>(4 ...captured: Captured5) => `(?${Type}${StringJoin<[...Captured[]], "", `${[...Captured[]][0]}`, [0]>})`;
Helper for all groups that start with ?
.
Type parameters
Type parameter |
---|
Type extends Strigifiable |
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
Function
Type parameters
Type parameter |
---|
Captured extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…captured | Captured |
Returns
`(?${Type}${StringJoin<[…Captured[]], "", `${[…Captured[]][0]}`, [0]>})`
capturedNumber()
1function capturedNumber<GroupNumber>(groupNumber: GroupNumber): $ {2 GroupNumber;3}
Matches the results of a capture group. For example \1 matches the results of the first capture group & \3 matches the third.
Type parameters
Type parameter |
---|
GroupNumber extends number |
Parameters
Parameter | Type |
---|---|
groupNumber | GroupNumber |
Returns
\${GroupNumber}
group()
1function group<Captured>(2 ...captured: Captured3): `(?:${StringJoin<[...Captured[]], "", `${[...Captured[]][0]}`, [0]>})`;
Groups multiple tokens together without creating a capture group.
Type parameters
Type parameter |
---|
Captured extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…captured | Captured |
Returns
`(?:${StringJoin<[…Captured[]], "", `${[…Captured[]][0]}`, [0]>})`
Look around
captureNext()
1function captureNext<Captured>(2 ...captured: Captured3): `(?=${StringJoin<[...Captured[]], "", `${[...Captured[]][0]}`, [0]>})`;
Matches a group after the main expression without including it in the result.
Type parameters
Type parameter |
---|
Captured extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…captured | Captured |
Returns
`(?=${StringJoin<[…Captured[]], "", `${[…Captured[]][0]}`, [0]>})`
capturePrevious()
1function capturePrevious<Captured>(2 ...captured: Captured3): `(?<=${StringJoin<[...Captured[]], "", `${[...Captured[]][0]}`, [0]>})`;
Matches a group before the main expression without including it in the
result.
Type parameters
Type parameter |
---|
Captured extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…captured | Captured |
Returns
`(?<=${StringJoin<[…Captured[]], "", `${[…Captured[]][0]}`, [0]>})`
notCaptureNext()
1function notCaptureNext<Captured>(2 ...captured: Captured3): `(?!${StringJoin<[...Captured[]], "", `${[...Captured[]][0]}`, [0]>})`;
Specifies a group that can not match after the main expression (if it matches, the result is discarded).
Type parameters
Type parameter |
---|
Captured extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…captured | Captured |
Returns
`(?!${StringJoin<[…Captured[]], "", `${[…Captured[]][0]}`, [0]>})`
notCapturePrevious()
1function notCapturePrevious<Captured>(2 ...captured: Captured3): `(?<!${StringJoin<[...Captured[]], "", `${[...Captured[]][0]}`, [0]>})`;
Specifies a group that can not match before the main expression (if it matches, the result is discarded).
Type parameters
Type parameter |
---|
Captured extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…captured | Captured |
Returns
`(?<!${StringJoin<[…Captured[]], "", `${[…Captured[]][0]}`, [0]>})`
Quantifiers & Alternation
allow()
1function allow<Token>(token: Token): `${Token}*`;
Matches 0 or more of the preceding token.
Type parameters
Type parameter |
---|
Token extends Strigifiable |
Parameters
Parameter | Type |
---|---|
token | Token |
Returns
`${Token}*`
exists()
1function exists<Token>(token: Token): `${Token}+`;
Matches 1 or more of the preceding token.
Type parameters
Type parameter |
---|
Token extends Strigifiable |
Parameters
Parameter | Type |
---|---|
token | Token |
Returns
`${Token}+`
optional()
1function optional<Token>(token: Token): `${Token}?`;
Matches 0 or 1 of the preceding token, effectively making it optional. Also makes the preceding quantifier lazy, causing it to match as few characters as possible. By default, quantifiers are greedy, and will match as many characters as possible.
Type parameters
Type parameter |
---|
Token extends Strigifiable |
Parameters
Parameter | Type |
---|---|
token | Token |
Returns
`${Token}?`
or()
1function or<Tokens>(...tokens: Tokens): StringJoin<Tokens, "|">;
Acts like a boolean OR. Matches the expression before or after the |. It can operate within a group, or on a whole expression. The patterns will be tested in order.
Type parameters
Type parameter |
---|
Tokens extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…tokens | Tokens |
Returns
StringJoin
<Tokens
, ”|”>
quantity()
1function quantity<Quantities>(2 quantities: Quantities,3): <Token>(token: Token) => `${Token}{${Quantities}}`;
Matches the specified quantity of the previous token. {1,3} will match 1 to 3. {3} will match exactly 3. {3,} will match 3 or more.
Type parameters
Type parameter |
---|
Quantities extends Strigifiable |
Parameters
Parameter | Type |
---|---|
quantities | Quantities |
Returns
Function
Type parameters
Type parameter |
---|
Token extends string |
Parameters
Parameter | Type |
---|---|
token | Token |
Returns
`${Token}{${Quantities}}`
Util
build()
1function build<Flags>(2 flags: Flags | "u",3): <Tokens>(...tokens: Tokens) => RegExp & object;
Builds a RegExp
with required u
flag and strongly typed source (using
join).
Type parameters
Type parameter | Value |
---|
| Flags
extends | "u"
| "su"
| "mu"
| "msu"
| "iu"
| "isu"
| "imu"
| "imsu"
| "gu"
| "gsu"
| "gmu"
| "gmsu"
| "giu"
|
"gisu"
| "gimu"
| "gimsu"
| "u"
|
Parameters
Parameter | Type | Default value |
---|---|---|
flags | Flags | "u" | "u" |
Returns
Function
Type parameters
Type parameter |
---|
Tokens extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…tokens | Tokens |
Returns
RegExp
& object
escape()
1function escape<Escaped>(escaped: Escaped): $ {2 Escaped;3}
Escape given string with a “.
Type parameters
Type parameter |
---|
Escaped extends Strigifiable |
Parameters
Parameter | Type | Description |
---|---|---|
escaped | Escaped | Value to escape. |
Returns
\${Escaped}
Escaped value.
Example
1escape("n"); // "\\n"
join()
1function join<Tokens>(...tokens: Tokens): StringJoin<Tokens, "">;
Util to join strings.
Type parameters
Type parameter |
---|
Tokens extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…tokens | Tokens |
Returns
StringJoin
<Tokens
, ""
>