jsre.nim 4.2 KB

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