jsre.nim 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. ## Regular Expressions for the JavaScript target.
  2. ## * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
  3. ##
  4. ## Examples
  5. ## ========
  6. ##
  7. ## .. code-block::nim
  8. ## let jsregex: RegExp = newRegExp(r"\s+", r"i")
  9. ## jsregex.compile(r"\w+", r"i")
  10. ## doAssert jsregex.test(r"nim javascript")
  11. ## doAssert jsregex.exec(r"nim javascript") == @["nim".cstring]
  12. ## doAssert jsregex.toString() == r"/\w+/i"
  13. ## jsregex.compile(r"[0-9]", r"i")
  14. ## doAssert jsregex.test(r"0123456789abcd")
  15. when not defined(js) and not defined(Nimdoc):
  16. {.error: "This module only works on the JavaScript platform".}
  17. type RegExp* {.importjs.} = object ## Regular Expressions for JavaScript target.
  18. flags* {.importjs.}: cstring ## cstring that contains the flags of the RegExp object.
  19. dotAll* {.importjs.}: bool ## Whether `.` matches newlines or not.
  20. global* {.importjs.}: bool ## Whether to test against all possible matches in a string, or only against the first.
  21. ignoreCase* {.importjs.}: bool ## Whether to ignore case while attempting a match in a string.
  22. multiline* {.importjs.}: bool ## Whether to search in strings across multiple lines.
  23. source* {.importjs.}: cstring ## The text of the pattern.
  24. sticky* {.importjs.}: bool ## Whether the search is sticky.
  25. unicode* {.importjs.}: bool ## Whether Unicode features are enabled.
  26. lastIndex* {.importjs.}: cint ## Index at which to start the next match (read/write property).
  27. input* {.importjs.}: cstring ## Read-only and modified on successful match.
  28. lastMatch* {.importjs.}: cstring ## Ditto.
  29. lastParen* {.importjs.}: cstring ## Ditto.
  30. leftContext* {.importjs.}: cstring ## Ditto.
  31. rightContext* {.importjs.}: cstring ## Ditto.
  32. func newRegExp*(pattern: cstring; flags: cstring): RegExp {.importjs: "new RegExp(@)".}
  33. ## Creates a new RegExp object.
  34. func compile*(self: RegExp; pattern: cstring; flags: cstring) {.importjs: "#.compile(@)".}
  35. ## Recompiles a regular expression during execution of a script.
  36. func exec*(self: RegExp; pattern: cstring): seq[cstring] {.importjs: "#.exec(#)".}
  37. ## Executes a search for a match in its string parameter.
  38. func test*(self: RegExp; pattern: cstring): bool {.importjs: "#.test(#)".}
  39. ## Tests for a match in its string parameter.
  40. func toString*(self: RegExp): cstring {.importjs: "#.toString()".}
  41. ## Returns a string representing the RegExp object.