在 Clojure 中,我可以使用类似这样的解决方案:Compact Clojure code for regular expression matchs and their position in string,即创建 are-matcher
并从中提取信息,但重新匹配器似乎没有在 ClojureScript 中实现。在 ClojureScript 中完成同样事情的好方法是什么?
编辑:
我最终编写了一个补充函数,以保留正则表达式的修饰符,因为它被吸收到re-pos
:
(defn regex-modifiers
"Returns the modifiers of a regex, concatenated as a string."
[re]
(str (if (.-multiline re) "m")
(if (.-ignoreCase re) "i")))
(defn re-pos
"Returns a vector of vectors, each subvector containing in order:
the position of the match, the matched string, and any groups
extracted from the match."
[re s]
(let [re (js/RegExp. (.-source re) (str "g" (regex-modifiers re)))]
(loop [res []]
(if-let [m (.exec re s)]
(recur (conj res (vec (cons (.-index m) m))))
res))))