In Clojure I could use something like this solution: Compact Clojure code for regular expression matches and their position in string, i.e., creating a re-matcher
and extracted the information from that, but re-matcher doesn't appear to be implemented in ClojureScript. What would be a good way to accomplish the same thing in ClojureScript?
在Clojure中我可以使用类似这样的解决方案:用于正则表达式匹配的紧凑Clojure代码及其在字符串中的位置,即创建重新匹配器并从中提取信息,但重新匹配器似乎不是在ClojureScript中实现的。什么是在ClojureScript中完成同样的事情的好方法?
Edit:
I ended up writing a supplementary function in order to preserve the modifiers of the regex as it is absorbed into 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))))
1 个解决方案
#1
8
You can use the .exec
method of JS RegExp
object. The returned match object contains an index
property that corresponds to the index of the match in the string.
您可以使用JS RegExp对象的.exec方法。返回的匹配对象包含一个index属性,该属性对应于字符串中匹配的索引。
Currently clojurescript doesn't support constructing regex literals with the g
mode flag (see CLJS-150), so you need to use the RegExp
constructor. Here is a clojurescript implementation of the re-pos
function from the linked page:
目前clojurescript不支持使用g mode标志构造正则表达式文字(请参阅CLJS-150),因此您需要使用RegExp构造函数。这是来自链接页面的重新发布函数的clojurescript实现:
(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"}
#1
8
You can use the .exec
method of JS RegExp
object. The returned match object contains an index
property that corresponds to the index of the match in the string.
您可以使用JS RegExp对象的.exec方法。返回的匹配对象包含一个index属性,该属性对应于字符串中匹配的索引。
Currently clojurescript doesn't support constructing regex literals with the g
mode flag (see CLJS-150), so you need to use the RegExp
constructor. Here is a clojurescript implementation of the re-pos
function from the linked page:
目前clojurescript不支持使用g mode标志构造正则表达式文字(请参阅CLJS-150),因此您需要使用RegExp构造函数。这是来自链接页面的重新发布函数的clojurescript实现:
(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"}