#"" regex

syntaxsince v0.0-1853 in clojureEdit

Regular expressions compile to native JavaScript regular expressions.

  • #"..." => js /.../
  • #"(?i)..." => js /.../i (case-insensitive)
  • #"(?m)..." => js /.../m (multi-line)

Details:

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.


Examples:

#"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")

See Also:


Reader code @ tools.reader:src/main/clojure/clojure/tools/reader.clj
(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))))))))

Reader table @ tools.reader:src/main/clojure/clojure/tools/reader.clj
(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))