syntax | since v0.0-1853 | in clojure | Edit |
Regular expressions compile to native JavaScript regular expressions.
#"..."
=> js /.../
#"(?i)..."
=> js /.../i
(case-insensitive)#"(?m)..."
=> js /.../m
(multi-line)The concept of "global match" (i.e. /.../g
in JavaScript) is not encoded
in ClojureScript regular expressions. Instead, use a function like
re-seq
to perform global matches.
#"foo"
;;=> #"foo"
(re-seq #"foo" "FOO BAR foo bar")
;;=> ("foo")
Case-insensitive matching:
#"(?i)foo"
;;=> #"foo"
(re-seq #"(?i)foo" "FOO BAR foo bar")
;;=> ("FOO" "foo")
(defn read-regex
[rdr ch opts pending-forms]
(let [sb (StringBuilder.)]
(loop [ch (read-char rdr)]
(if (identical? \" ch)
(Pattern/compile (str sb))
(if (nil? ch)
(err/throw-eof-reading rdr :regex sb)
(do
(.append sb ch )
(when (identical? \\ ch)
(let [ch (read-char rdr)]
(if (nil? ch)
(err/throw-eof-reading rdr :regex sb))
(.append sb ch)))
(recur (read-char rdr))))))))
(defn- dispatch-macros [ch]
(case ch
\^ read-meta ;deprecated
\' (wrapping-reader 'var)
\( read-fn
\= read-eval
\{ read-set
\< (throwing-reader "Unreadable form")
\" read-regex
\! read-comment
\_ read-discard
\? read-cond
\: read-namespaced-map
\# read-symbolic-value
nil))