Entity, that might be used as a pattern, namely: primitive string
, primitive number
, or an instance of RegExp
.
Notice how RegExp
is a subtype of Pattern
in this nomenclature.
This documentation heavily relies on the definition of Pattern
, but nevertheless you don't necessarily have to import and use it explicitly.
Concatenate several input patterns into a single RegExp.
combined(/a/g, /b/s);
// /ab/gs
combined("a", /b/);
// /ab/
Note: here and later, in all the functions that take in several patterns as arguments list (i.e. whose signature is similar to f(...pattern: Pattern[])
) will accumulate and intelligently combine all the flags from all the given patterns. To remove unwanted flags, use combinedWithoutFlags
function (see below).
Concatenate several input patterns into a single RegExp, and add given flags to it.
combinedWithFlags("g")("a", /b/);
// /ab/g
combinedWithFlags("gs")("a", /b/);
// /ab/gs
Concatenate several input patterns into a single RegExp, and remove given flags from it.
combinedWithoutFlags("g")("a", /b/g);
// /ab/
combinedWithoutFlags("gs")("a", /b/gm);
// /ab/m
combinedWithoutFlags()("a", /b/g);
// /ab/g
Notice how in the third example no flags were removed, which means that you have to specify all the unwanted flags exactly.
Expect the pattern to be a whole string, rather than a part of it.
detached("a", /b/);
// /^ab$/
Expect appearance of one of the given patterns.
eitherOf("abc", "def");
// /(?:abc|def)/
eitherOf("a", /b/);
// /(?:a|b)/
Notice that in the second example a character set is a more optimal solution, so in the case of single characters prefer using eitherOfChars()
function or regex literal [ab]
if possible.
Expect appearance of one of the characters given in the input. The argument(s) of this function must be primitive string(s), it is an error to provide arguments of other types.
eitherOfChars("a", "B");
// /[aB]/
eitherOfChars("chAracters");
// /[chArates]/
eitherOfChars("hello", "world");
// /[helowrd]/
eitherOfChars(/a/g, /b/i);
// Validation failed: value is not primitive: value <RegExp> /a/g
eitherOfChars(42, 17);
// Validation failed: value is not of type "string": value <number> 42
Also available as enclosedIn.curlyBraces
.
Enclose pattern(s) in {}
curly braces.
enclosedInCurlyBraces("a", /b/);
// /\{ab\}/
import { enclosedIn } from "re-scaled";
enclosedIn.curlyBraces(/a/g, /b/s);
// /\{ab\}/gs
Also available as enclosedIn.parentheses
.
Enclose pattern(s) in ()
round brackets.
enclosedInParentheses("a", /b/);
// /\(ab\)/
import { enclosedIn } from "re-scaled";
enclosedIn.parentheses(/a/g, /b/s);
// /\(ab\)/gs
Also available as enclosedIn.squareBrackets
.
Enclose pattern(s) in []
square brackets.
enclosedInSquareBrackets("a", /b/);
// /\[ab\]/
import { enclosedIn } from "re-scaled";
enclosedIn.squareBrackets(/a/g, /b/s);
// /\[ab\]/gs
Group several input patterns into a non-capturing group.
grouped("a", /b/);
// /(?:ab)/
Expect at least one appearance of the given pattern.
oneOrMore("a", /b/);
// /(?:ab)+/
Make pattern optional.
optional("a", /b/);
// /(?:ab)?/
Also available as repeated.atLeast
.
Repeat pattern at least count
amount of times. The value of count
must be a non-negative natural number (0
, 1
, 2
etc.). This is being internally checked by the package @valuer/main
, so any invalid value will cause an error to be thrown.
repeatedAtLeast(3)("a", /b/);
// /(?:ab){3,}/
import { repeated } from "re-scaled";
repeated.atLeast(10)(/c/g, /d/s);
// /(?:cd){10,}/gs
repeatedAtLeast(-7)("a", /b/);
// Validation failed: min repeat count should not be a negative number: value <number> -7
Note that in the case of invalid count
, the error prevents the intermediate function from being created.
Also available as repeated.between
.
Repeat pattern between min
and max
amount of times inclusively. The values of both min
and max
must be non-negative natural numbers (0
, 1
, 2
etc.). This is being internally checked by the package @valuer/main
, so any invalid value will cause an error to be thrown.
repeatedBetween(2, 3)("a", /b/);
// /(?:ab){2,3}/
repeatedBetween(3, 2)("c");
// /(?:c){2,3}/
Notice how the reverse range is intelligently taken care of in this example.
import { repeated } from "re-scaled";
repeated.between(17, 42)(/d/, "e");
// /(?:d){17,42}/
repeatedBetween(-1, 2)(/f/, /g/);
// Validation failed: min repeat count should not be a negative number: value <number> -1
repeatedBetween(1, 2.5)(/f/, /g/);
// Validation failed: max repeat count is not an integer: value <number> 2.5
Note that in the case of invalid min
and/or max
, the error prevents the intermediate function from being created.
Also available as repeated.twice
.
Repeat pattern exactly twice.
repeatedTwice("a", /b/);
// /(?:ab){2}/
import { repeated } from "re-scaled";
repeated.twice(/c/, /d/);
// /(?:cd){2}/
Also available as repeated.thrice
.
Repeat pattern exactly three times.
repeatedThrice("a", /b/);
// /(?:ab){3}/
import { repeated } from "re-scaled";
repeated.thrice(/c/, /d/);
// /(?:cd){3}/
Also available as repeated.times
.
Repeat pattern exactly count
amount of times. The value of count
must be a non-negative natural number (0
, 1
, 2
etc.). This is being internally checked by the package @valuer/main
, so any invalid value will cause an error to be thrown.
repeatedTimes(42)("a", /b/);
// /(?:ab){42}/
import { repeated } from "re-scaled";
repeated.times(17)(/c/, /d/);
// /(?:cd){17}/
repeatedTimes("five")("e", /f/);
// Validation failed: repeat count is not an integer: value <string> five
Note that in the case of invalid count
, the error prevents the intermediate function from being created.
Concatenate several input patterns into a single RegExp, separating them by a given separator
pattern.
separatedBy(", ")(1, 2, 3);
// /(?:1, 2, 3)/
separatedBy("|")("hello", "hola");
// /(?:hello|hola)/
Concatenate several input patterns into a single RegExp and store the result under a given name.
This function uses a new ES2018 feature, called Named Capturing Groups.
storedAs("word")("a", "b", "c");
// /(?<word>abc)/