summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorJuan Carlos <juancarlospaco@gmail.com>2022-06-28 03:13:17 -0300
committerGitHub <noreply@github.com>2022-06-28 08:13:17 +0200
commit7c31b6a47b5cfb6f2d1f296390eb457bd75e55c2 (patch)
treea2cdd6813f541ebc3021bf74120cd770f30ae00a /lib
parent0189122d4fadc73a3de18a7a781997c506f837aa (diff)
downloadNim-7c31b6a47b5cfb6f2d1f296390eb457bd75e55c2.tar.gz
Fix jsre (#19917)
* Fixes for jsre to make it more safe at runtime on some edge cases

* https://github.com/nim-lang/Nim/pull/19917#issuecomment-1162692893
Diffstat (limited to 'lib')
-rw-r--r--lib/js/jsre.nim8
1 files changed, 5 insertions, 3 deletions
diff --git a/lib/js/jsre.nim b/lib/js/jsre.nim
index cd8fb2be7..0cc2f8a87 100644
--- a/lib/js/jsre.nim
+++ b/lib/js/jsre.nim
@@ -33,13 +33,13 @@ 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 split*(pattern: cstring; self: RegExp): seq[cstring] {.importjs: "#.split(#)".}
+func split*(pattern: cstring; self: RegExp): seq[cstring] {.importjs: "(#.split(#) || [])".}
   ## Divides a string into an ordered list of substrings and returns the array
 
-func match*(pattern: cstring; self: RegExp): seq[cstring] {.importjs: "#.match(#)".}
+func match*(pattern: cstring; self: RegExp): seq[cstring] {.importjs: "(#.match(#) || [])".}
   ## Returns an array of matches of a RegExp against given string
 
-func exec*(self: RegExp; pattern: cstring): seq[cstring] {.importjs: "#.exec(#)".}
+func exec*(self: RegExp; pattern: cstring): seq[cstring] {.importjs: "(#.exec(#) || [])".}
   ## Executes a search for a match in its string parameter.
 
 func toCstring*(self: RegExp): cstring {.importjs: "#.toString()".}
@@ -87,3 +87,5 @@ runnableExamples:
   assert "do1ne".split(jsregex) == @["do".cstring, "ne".cstring]
   jsregex.compile(r"[lw]", r"i")
   assert "hello world".replace(jsregex,"X") == "heXlo world"
+  let digitsRegex: RegExp = newRegExp(r"\d")
+  assert "foo".match(digitsRegex) == @[]