function | since v0.0-927 | clojure.string/replace | Edit |
(replace s match replacement)
Replaces all instance of match
with replacement
in s
.
The options for match / replacement are:
match | replacement |
---|---|
string | string |
regex | string |
regex | function |
Replaces all instance of match with replacement in s. match/replacement can be: string / string pattern / (string or function of match). See also replace-first. The replacement is literal (i.e. none of its characters are treated specially) for all cases above except pattern / string. For pattern / string, $1, $2, etc. in the replacement string are substituted with the string that matched the corresponding parenthesized group in the pattern. Example: (clojure.string/replace "Almost Pig Latin" #"\b(\w)(\w+)\b" "$2$1ay") -> "lmostAay igPay atinLay"
(defn ^string replace
[s match replacement]
(cond
(string? match)
(.replace s (js/RegExp. (gstring/regExpEscape match) "g") replacement)
(instance? js/RegExp match)
(if (string? replacement)
(replace-all s match replacement)
(replace-all s match (replace-with replacement)))
:else (throw (str "Invalid match arg: " match))))