wreck.api
The public API of wreck
.
Notes:
- Apart from passing through
nil
, this library does minimal argument checking, since the rules for regular expressions vary from platform to platform, and it is a first class requirement that callers be allowed to construct platform specific regular expressions if they wish. - As a result, all functions have the potential to throw platform-specific exceptions if the resulting regular expression is syntactically invalid.
- On the JVM, these will typically be instances of the
java.util.regex.PatternSyntaxException
class. - On JavaScript, these will typically be a
js/SyntaxError
. - Platform specific behaviour is particularly notable for short / empty regular expressions, such as
#"{}"
(an error on the JVM, fine but nonsensical on JS) and#"{1}"
(ironically, fine but nonsensical on the JVM, but an error on JS). 🤡 - Furthemore, JavaScript fundamentally doesn’t support lossless round-tripping of
RegExp
objects toString
s and back, something this library relies upon and does extensively. The library makes a best effort to correct JavaScript’s problematic implementation, but because it’s fundamentally lossy there are some cases that (on ClojureScript only) may change your regexes in unexpected (though not semantically significant) ways.
='
(=' _)
(=' re1 re2)
(=' re1 re2 & more)
Equality for regexes, defined by having equal String
representations. This means that equivalent regexes (e.g. #"..."
and #".{3}"
will not be considered equal.
Notes:
- Some JavaScript runtimes that ClojureScript runs on correctly implement equality for regexes, but the JVM does not.
alt
(alt & res)
Returns a regex that will match any one of res
, via alternation.
Notes:
- Duplicate elements in
res
will only appear once in the result. - Does not wrap the result in a group, which, because alternation has the lowest precedence in regexes, runs the risk of behaving unexpectedly if the result is then combined with further regexes. tl;dr - one of the grouping variants should almost always be preferred.
and'
(and' a b)
(and' a b s)
Returns an ‘and’ regex that will match a
and b
in any order, and with the s
eparator regex (if provided) between them. This is implemented as ASB|BSA
, which means that A and B must be distinct (must not match the same text).
Notes:
- May optimise the expression (via de-duplication in alt).
- Does not wrap the result in a group, which, because alternation has the lowest precedence in regexes, runs the risk of behaving unexpectedly if the result is then combined with further regexes. tl;dr - one of the grouping variants should almost always be preferred.
and-cg
(and-cg a b)
(and-cg a b s)
and-grp
(and-grp a b)
(and-grp a b s)
and-ncg
(and-ncg nm a b)
(and-ncg nm a b s)
esc
(esc s)
Escapes s
(a String
) for use in a regex, returning a String
. Note that unlike most other fns in this namespace, this one does not support a regex as an input, nor return a regex as an output.
join
(join & res)
Returns a regex that is all of the res
joined together. Each element in res
can be a regex, a String
or something that can be turned into a String
(including numbers, etc.). Returns nil
when no res
are provided, or they’re all nil
.
Notes:
- In ClojureScript be cautious about using numbers in these calls, since JavaScript’s number handling is a 🤡show. See this unit test for a worked example of the types of problems that can occur.
ncg
(ncg nm & res)
As for grp, but uses a named capturing group named nm
. Returns nil
if nm
is nil
or blank. Throws if nm
is an invalid name for a named capturing group (alphanumeric only, must start with an alphabetical character, must be unique within the regex).
or'
(or' a b)
(or' a b s)
Returns an ‘inclusive or’ regex that will match a
or b
, or both, in any order, and with the s
eparator regex (if provided) between them. This is implemented as ASB|BSA|A|B
, which means that A and B must be distinct (must not match the same text).
Notes:
- May optimise the expression (via de-duplication in alt).
- Does not wrap the result in a group, which, because alternation has the lowest precedence in regexes, runs the risk of behaving unexpectedly if the result is then combined with further regexes. tl;dr - one of the grouping variants should almost always be preferred.
or-cg
(or-cg a b)
(or-cg a b s)
or-grp
(or-grp a b)
(or-grp a b s)
or-ncg
(or-ncg nm a b)
(or-ncg nm a b s)
str'
(str' o)
Returns the String
representation of o
, with special handling for RegExp
objects on ClojureScript in an attempt to correct JavaScript’s APPALLING default stringification.
xor'
(xor' a b)
Returns an ‘exclusive or’ regex that will match a
or b
, but not both. This is identical to alt called with 2 arguments, and is provided as a convenience for those who might be building up large logic based regexes and would prefer to use more easily understood logical operator names throughout.
Notes:
- May optimise the expression (via de-duplication in alt).
- Does not wrap the result in a group, which, because alternation has the lowest precedence in regexes, runs the risk of behaving unexpectedly if the result is then combined with further regexes. tl;dr - one of the grouping variants should almost always be preferred.
xor-cg
(xor-cg a b)
xor-grp
(xor-grp a b)
xor-ncg
(xor-ncg nm a b)