Functional Expression Reference
🧙 Functional Regular expression builder.
Usage
📦 Node
Install functional-expression
as a dependency:
Import it and use it:
🦕 Deno
Import functional-expression
using the npm:
prefix, and use it directly:
🌎 Browser
Import functional-expression
using esm.sh, and use it directly:
Useful links
- 📝 Documentation: TypeDoc generated documentation.
- ⏳ Changelog: List of changes between versions.
- ✅ Tests Coverage: Coveralls page with tests coverage.
Anchors
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
Matches any position that is not a word boundary. This matches a position, not a character.
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
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
Matches any character except linebreaks. Equivalent to [^\n\r].
DIGIT
Matches any digit character (0-9). Equivalent to [0-9].
NOT_DIGIT
Matches any character that is not a digit character (0-9). Equivalent to [^0-9].
NOT_WHITESPACE
Matches any character that is not a whitespace character (spaces, tabs, line breaks).
NOT_WORD
Matches any character that is not a word character (alphanumeric & underscore). Equivalent to [^A-Za-z0-9_]
WHITESPACE
Matches any whitespace character (spaces, tabs, line breaks).
WORD
Matches any word character (alphanumeric & underscore). Only matches low-ascii characters (no accented or non-roman characters). Equivalent to [A-Za-z0-9_]
notSet()
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()
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()
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()
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()
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
Possible digits for an hexadecimal number.
UppercaseLetters
Uppercase letter values.
CARRIAGE_RETURN
Matches a CARRIAGE RETURN character (char code 13).
FORM_FEED
Matches a FORM FEED character (char code 12).
LINE_FEED
Matches a LINE FEED character (char code 10).
NULL
Matches a NULL character (char code 0).
TAB
Matches a TAB character (char code 9).
VERTICAL_TAB
Matches a VERTICAL TAB character (char code 11).
controlCharacter()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
Matches 0 or more of the preceding token.
Type parameters
Type parameter |
---|
Token extends Strigifiable |
Parameters
Parameter | Type |
---|---|
token | Token |
Returns
`${Token}*`
exists()
Matches 1 or more of the preceding token.
Type parameters
Type parameter |
---|
Token extends Strigifiable |
Parameters
Parameter | Type |
---|---|
token | Token |
Returns
`${Token}+`
optional()
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()
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()
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()
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()
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
join()
Util to join strings.
Type parameters
Type parameter |
---|
Tokens extends ReadOnlyArray <Strigifiable > |
Parameters
Parameter | Type |
---|---|
…tokens | Tokens |
Returns
StringJoin
<Tokens
, ""
>