jsre.nim 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ## Regular Expressions for the JavaScript target.
  2. ## * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
  3. when not defined(js):
  4. {.error: "This module only works on the JavaScript platform".}
  5. type RegExp* = ref object of JsRoot
  6. ## Regular Expressions for JavaScript target.
  7. ## See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
  8. flags*: cstring ## cstring that contains the flags of the RegExp object.
  9. dotAll*: bool ## Whether `.` matches newlines or not.
  10. global*: bool ## Whether to test against all possible matches in a string, or only against the first.
  11. ignoreCase*: bool ## Whether to ignore case while attempting a match in a string.
  12. multiline*: bool ## Whether to search in strings across multiple lines.
  13. source*: cstring ## The text of the pattern.
  14. sticky*: bool ## Whether the search is sticky.
  15. unicode*: bool ## Whether Unicode features are enabled.
  16. lastIndex*: cint ## Index at which to start the next match (read/write property).
  17. input*: cstring ## Read-only and modified on successful match.
  18. lastMatch*: cstring ## Ditto.
  19. lastParen*: cstring ## Ditto.
  20. leftContext*: cstring ## Ditto.
  21. rightContext*: cstring ## Ditto.
  22. hasIndices*: bool ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices
  23. func newRegExp*(pattern: cstring; flags: cstring): RegExp {.importjs: "new RegExp(@)".}
  24. ## Creates a new RegExp object.
  25. func newRegExp*(pattern: cstring): RegExp {.importjs: "new RegExp(@)".}
  26. func compile*(self: RegExp; pattern: cstring; flags: cstring) {.importjs: "#.compile(@)".}
  27. ## Recompiles a regular expression during execution of a script.
  28. func replace*(pattern: cstring; self: RegExp; replacement: cstring): cstring {.importjs: "#.replace(#, #)".}
  29. ## Returns a new string with some or all matches of a pattern replaced by given replacement
  30. func replace*(pattern: cstring, self: RegExp, cb: proc (args: varargs[cstring]): cstring): cstring {.importcpp.}
  31. ## Returns a new string with some or all matches of a pattern replaced by given callback function
  32. func split*(pattern: cstring; self: RegExp): seq[cstring] {.importjs: "(#.split(#) || [])".}
  33. ## Divides a string into an ordered list of substrings and returns the array
  34. func match*(pattern: cstring; self: RegExp): seq[cstring] {.importjs: "(#.match(#) || [])".}
  35. ## Returns an array of matches of a RegExp against given string
  36. func exec*(self: RegExp; pattern: cstring): seq[cstring] {.importjs: "(#.exec(#) || [])".}
  37. ## Executes a search for a match in its string parameter.
  38. func toCstring*(self: RegExp): cstring {.importjs: "#.toString()".}
  39. ## Returns a string representing the RegExp object.
  40. func `$`*(self: RegExp): string = $toCstring(self)
  41. func contains*(pattern: cstring; self: RegExp): bool =
  42. ## Tests for a substring match in its string parameter.
  43. runnableExamples:
  44. let jsregex: RegExp = newRegExp(r"bc$", r"i")
  45. assert jsregex in r"abc"
  46. assert jsregex notin r"abcd"
  47. assert "xabc".contains jsregex
  48. asm "`result` = `self`.test(`pattern`);"
  49. func startsWith*(pattern: cstring; self: RegExp): bool =
  50. ## Tests if string starts with given RegExp
  51. runnableExamples:
  52. let jsregex: RegExp = newRegExp(r"abc", r"i")
  53. assert "abcd".startsWith jsregex
  54. pattern.contains(newRegExp(("^" & $(self.source)).cstring, self.flags))
  55. func endsWith*(pattern: cstring; self: RegExp): bool =
  56. ## Tests if string ends with given RegExp
  57. runnableExamples:
  58. let jsregex: RegExp = newRegExp(r"bcd", r"i")
  59. assert "abcd".endsWith jsregex
  60. pattern.contains(newRegExp(($(self.source) & "$").cstring, self.flags))
  61. runnableExamples:
  62. let jsregex: RegExp = newRegExp(r"\s+", r"i")
  63. jsregex.compile(r"\w+", r"i")
  64. assert "nim javascript".contains jsregex
  65. assert jsregex.exec(r"nim javascript") == @["nim".cstring]
  66. assert jsregex.toCstring() == r"/\w+/i"
  67. jsregex.compile(r"[0-9]", r"i")
  68. assert "0123456789abcd".contains jsregex
  69. assert $jsregex == "/[0-9]/i"
  70. jsregex.compile(r"abc", r"i")
  71. assert "abcd".startsWith jsregex
  72. assert "dabc".endsWith jsregex
  73. jsregex.compile(r"\d", r"i")
  74. assert "do1ne".split(jsregex) == @["do".cstring, "ne".cstring]
  75. jsregex.compile(r"[lw]", r"i")
  76. assert "hello world".replace(jsregex,"X") == "heXlo world"
  77. jsregex.compile(r"([a-z])\1*", r"g")
  78. assert "abbcccdddd".replace(jsregex, proc (m: varargs[cstring]): cstring = ($m[0] & $(m.len)).cstring) == "a1b2c3d4"
  79. let digitsRegex: RegExp = newRegExp(r"\d")
  80. assert "foo".match(digitsRegex) == @[]