jsre.nim 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. func newRegExp*(pattern: cstring; flags: cstring): RegExp {.importjs: "new RegExp(@)".}
  23. ## Creates a new RegExp object.
  24. func newRegExp*(pattern: cstring): RegExp {.importjs: "new RegExp(@)".}
  25. func compile*(self: RegExp; pattern: cstring; flags: cstring) {.importjs: "#.compile(@)".}
  26. ## Recompiles a regular expression during execution of a script.
  27. func replace*(pattern: cstring; self: RegExp; replacement: cstring): cstring {.importjs: "#.replace(#, #)".}
  28. ## Returns a new string with some or all matches of a pattern replaced by given replacement
  29. func split*(pattern: cstring; self: RegExp): seq[cstring] {.importjs: "#.split(#)".}
  30. ## Divides a string into an ordered list of substrings and returns the array
  31. func match*(pattern: cstring; self: RegExp): seq[cstring] {.importjs: "#.match(#)".}
  32. ## Returns an array of matches of a RegExp against given string
  33. func exec*(self: RegExp; pattern: cstring): seq[cstring] {.importjs: "#.exec(#)".}
  34. ## Executes a search for a match in its string parameter.
  35. func toCstring*(self: RegExp): cstring {.importjs: "#.toString()".}
  36. ## Returns a string representing the RegExp object.
  37. func `$`*(self: RegExp): string = $toCstring(self)
  38. func test*(self: RegExp; pattern: cstring): bool {.importjs: "#.test(#)", deprecated: "Use contains instead".}
  39. func toString*(self: RegExp): cstring {.importjs: "#.toString()", deprecated: "Use toCstring instead".}
  40. func contains*(pattern: cstring; self: RegExp): bool =
  41. ## Tests for a substring match in its string parameter.
  42. runnableExamples:
  43. let jsregex: RegExp = newRegExp(r"bc$", r"i")
  44. assert jsregex in r"abc"
  45. assert jsregex notin r"abcd"
  46. assert "xabc".contains jsregex
  47. asm "`result` = `self`.test(`pattern`);"
  48. func startsWith*(pattern: cstring; self: RegExp): bool =
  49. ## Tests if string starts with given RegExp
  50. runnableExamples:
  51. let jsregex: RegExp = newRegExp(r"abc", r"i")
  52. assert "abcd".startsWith jsregex
  53. pattern.contains(newRegExp(("^" & $(self.source)).cstring, self.flags))
  54. func endsWith*(pattern: cstring; self: RegExp): bool =
  55. ## Tests if string ends with given RegExp
  56. runnableExamples:
  57. let jsregex: RegExp = newRegExp(r"bcd", r"i")
  58. assert "abcd".endsWith jsregex
  59. pattern.contains(newRegExp(($(self.source) & "$").cstring, self.flags))
  60. runnableExamples:
  61. let jsregex: RegExp = newRegExp(r"\s+", r"i")
  62. jsregex.compile(r"\w+", r"i")
  63. assert "nim javascript".contains jsregex
  64. assert jsregex.exec(r"nim javascript") == @["nim".cstring]
  65. assert jsregex.toCstring() == r"/\w+/i"
  66. jsregex.compile(r"[0-9]", r"i")
  67. assert "0123456789abcd".contains jsregex
  68. assert $jsregex == "/[0-9]/i"
  69. jsregex.compile(r"abc", r"i")
  70. assert "abcd".startsWith jsregex
  71. assert "dabc".endsWith jsregex
  72. jsregex.compile(r"\d", r"i")
  73. assert "do1ne".split(jsregex) == @["do".cstring, "ne".cstring]
  74. jsregex.compile(r"[lw]", r"i")
  75. assert "hello world".replace(jsregex,"X") == "heXlo world"