You can use the .exec
method of the JS RegExp
object. The return match object contains the index
property, which corresponds to the match index in the string.
Currently clojurescript does not support building regular expressions using the g
flag (see CLJS-150 ), so you need to use the RegExp
constructor. The following is an implementation of the re-pos
clojurescript function from the linked page:
(defn re-pos [re s] (let [re (js/RegExp. (.-source re) "g")] (loop [res {}] (if-let [m (.exec re s)] (recur (assoc res (.-index m) (first m))) res)))) cljs.user> (re-pos "\\w+" "The quick brown fox") {0 "The", 4 "quick", 10 "brown", 16 "fox"} cljs.user> (re-pos "[0-9]+" "3a1b2c1d") {0 "3", 2 "1", 4 "2", 6 "1"}
source share