Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by

When calling clojure.core/replace with a pattern and a function, the Clojurescript version delegates to Javascript's s.replace method, which calls that function with a variable number of arguments, depending on how many match groups are in your pattern. The Clojure version always calls it with a single argument, which may be a vector if you have match groups in your pattern.

I'm not sure if this was intentional. If it wasn't, I think this difference could be fixed through some use of re-find, which appears to return the same string or vector that you'd get in Clojure. If this is intentional for performance reasons, perhaps the doc string should be updated to note this, as there's no warning that the function is being called with too many arguments.

3 Answers

0 votes
by

Comment made by: cgag

Afraid I don't see how to edit, but I wanted to include a simple example:

CLJS:
(clojure.string/replace "hello world" #"(hello) world" (fn (link: m) (.log js/console (str "Match: " m)) m))

will log: "Match: hello world"

CLJ
user=> (clojure.string/replace "hello world" #"(hello) world" (fn (link: m) (println (str "Match: " m) m)))
Match: (link: "hello world" "hello") (link: hello world hello)

NullPointerException java.util.regex.Matcher.quoteReplacement (Matcher.java:655)

0 votes
by

Comment made by: mfikes

Perhaps this has been resolved. In ClojureScript 1.9.946:

cljs.user=> (clojure.string/replace "hello world" #"(hello) world" (fn [m] (println (str "Match: " m) m))) Match: ["hello world" "hello"] [hello world hello] "null"

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJS-746 (reported by alex+import)
...