summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorQinsi (James) ZHU <qszhu@users.noreply.github.com>2023-02-17 01:12:48 +0800
committerGitHub <noreply@github.com>2023-02-16 18:12:48 +0100
commit03dd853815173bcf63f80ef03b5a4db7177453ec (patch)
treeed13c9fe7dc832c69dffb06b6cedbe4caa11f79d /lib
parent3b9e9fd7b29de97b394811f350876732c66cff19 (diff)
downloadNim-03dd853815173bcf63f80ef03b5a4db7177453ec.tar.gz
add .replace() with callback to jsre (#21371)
Diffstat (limited to 'lib')
-rw-r--r--lib/js/jsre.nim5
1 files changed, 5 insertions, 0 deletions
diff --git a/lib/js/jsre.nim b/lib/js/jsre.nim
index 19888aaa9..69bd75c3b 100644
--- a/lib/js/jsre.nim
+++ b/lib/js/jsre.nim
@@ -34,6 +34,9 @@ func compile*(self: RegExp; pattern: cstring; flags: cstring) {.importjs: "#.com
 func replace*(pattern: cstring; self: RegExp; replacement: cstring): cstring {.importjs: "#.replace(#, #)".}
   ## Returns a new string with some or all matches of a pattern replaced by given replacement
 
+func replace*(pattern: cstring, self: RegExp, cb: proc (args: varargs[cstring]): cstring): cstring {.importcpp.}
+  ## Returns a new string with some or all matches of a pattern replaced by given callback function
+
 func split*(pattern: cstring; self: RegExp): seq[cstring] {.importjs: "(#.split(#) || [])".}
   ## Divides a string into an ordered list of substrings and returns the array
 
@@ -88,5 +91,7 @@ runnableExamples:
   assert "do1ne".split(jsregex) == @["do".cstring, "ne".cstring]
   jsregex.compile(r"[lw]", r"i")
   assert "hello world".replace(jsregex,"X") == "heXlo world"
+  jsregex.compile(r"([a-z])\1*", r"g")
+  assert "abbcccdddd".replace(jsregex, proc (m: varargs[cstring]): cstring = ($m[0] & $(m.len)).cstring) == "a1b2c3d4"
   let digitsRegex: RegExp = newRegExp(r"\d")
   assert "foo".match(digitsRegex) == @[]