Skip to content

Selecting RegExp values

Regular expressions can appear both as literals and as instances of the RegExp constructor. In the latter case, the pattern is actually a string literal, which makes it a little awkward if you're scanning for a particular pattern and don't care which form it was defined in.

(regex+ [pattern]) selects a regular expression pattern for you in either form (sans flags!) and is shorthand for:

clojure
(:or (regex \1)
     (of RegExp
         (arg 1 (str+ \1))))
(:or (regex \1)
     (of RegExp
         (arg 1 (str+ \1))))

Which, assuming we search for (regex+ foo), would match both of the following:

javascript
/foo/
new RegExp('foo')
/foo/
new RegExp('foo')

And any variations of the literal passed to RegExp that (str+) could match. You can also use a pattern for a pattern!

clojure
(regex+ /foo/)
(regex+ /foo/)

Which would match /aaafoobbb/ as well as new RegExp("aaafoobbb").

Copyright © 2022-present Semantic Works, Inc.