Source Edit

Regular Expressions for the JavaScript target.

Example:

  1. import std/jsre
  2. let jsregex: RegExp = newRegExp(r"\s+", r"i")
  3. jsregex.compile(r"\w+", r"i")
  4. assert "nim javascript".contains jsregex
  5. assert jsregex.exec(r"nim javascript") == @["nim".cstring]
  6. assert jsregex.toCstring() == r"/\w+/i"
  7. jsregex.compile(r"[0-9]", r"i")
  8. assert "0123456789abcd".contains jsregex
  9. assert $jsregex == "/[0-9]/i"
  10. jsregex.compile(r"abc", r"i")
  11. assert "abcd".startsWith jsregex
  12. assert "dabc".endsWith jsregex
  13. jsregex.compile(r"\d", r"i")
  14. assert "do1ne".split(jsregex) == @["do".cstring, "ne".cstring]
  15. jsregex.compile(r"[lw]", r"i")
  16. assert "hello world".replace(jsregex,"X") == "heXlo world"
  17. jsregex.compile(r"([a-z])\1*", r"g")
  18. assert "abbcccdddd".replace(jsregex, proc (m: varargs[cstring]): cstring = ($m[0] & $(m.len)).cstring) == "a1b2c3d4"
  19. let digitsRegex: RegExp = newRegExp(r"\d")
  20. assert "foo".match(digitsRegex) == @[]

Types

  1. RegExp = ref object of JsRoot
  2. flags*: cstring ## cstring that contains the flags of the RegExp object.
  3. dotAll*: bool ## Whether `.` matches newlines or not.
  4. global*: bool ## Whether to test against all possible matches in a string, or only against the first.
  5. ignoreCase*: bool ## Whether to ignore case while attempting a match in a string.
  6. multiline*: bool ## Whether to search in strings across multiple lines.
  7. source*: cstring ## The text of the pattern.
  8. sticky*: bool ## Whether the search is sticky.
  9. unicode*: bool ## Whether Unicode features are enabled.
  10. lastIndex*: cint ## Index at which to start the next match (read/write property).
  11. input*: cstring ## Read-only and modified on successful match.
  12. lastMatch*: cstring ## Ditto.
  13. lastParen*: cstring ## Ditto.
  14. leftContext*: cstring ## Ditto.
  15. rightContext*: cstring ## Ditto.
  16. hasIndices*: bool ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices

Regular Expressions for JavaScript target. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp Source Edit

Procs

  1. func `$`(self: RegExp): string {....raises: [], tags: [], forbids: [].}

Source Edit

  1. func compile(self: RegExp; pattern: cstring; flags: cstring) {.
  2. importjs: "#.compile(@)", ...raises: [], tags: [], forbids: [].}

Recompiles a regular expression during execution of a script. Source Edit

  1. func contains(pattern: cstring; self: RegExp): bool {....raises: [], tags: [],
  2. forbids: [].}

Tests for a substring match in its string parameter.

Example:

  1. let jsregex: RegExp = newRegExp(r"bc$", r"i")
  2. assert jsregex in r"abc"
  3. assert jsregex notin r"abcd"
  4. assert "xabc".contains jsregex

Source Edit

  1. func endsWith(pattern: cstring; self: RegExp): bool {....raises: [], tags: [],
  2. forbids: [].}

Tests if string ends with given RegExp

Example:

  1. let jsregex: RegExp = newRegExp(r"bcd", r"i")
  2. assert "abcd".endsWith jsregex

Source Edit

  1. func exec(self: RegExp; pattern: cstring): seq[cstring] {.
  2. importjs: "(#.exec(#) || [])", ...raises: [], tags: [], forbids: [].}

Executes a search for a match in its string parameter. Source Edit

  1. func match(pattern: cstring; self: RegExp): seq[cstring] {.
  2. importjs: "(#.match(#) || [])", ...raises: [], tags: [], forbids: [].}

Returns an array of matches of a RegExp against given string Source Edit

  1. func newRegExp(pattern: cstring): RegExp {.importjs: "new RegExp(@)",
  2. ...raises: [], tags: [], forbids: [].}

Source Edit

  1. func newRegExp(pattern: cstring; flags: cstring): RegExp {.
  2. importjs: "new RegExp(@)", ...raises: [], tags: [], forbids: [].}

Creates a new RegExp object. Source Edit

  1. func replace(pattern: cstring; self: RegExp;
  2. cb: proc (args: varargs[cstring]): cstring): cstring {.importcpp,
  3. ...raises: [], tags: [], forbids: [].}

Returns a new string with some or all matches of a pattern replaced by given callback function Source Edit

  1. func replace(pattern: cstring; self: RegExp; replacement: cstring): cstring {.
  2. importjs: "#.replace(#, #)", ...raises: [], tags: [], forbids: [].}

Returns a new string with some or all matches of a pattern replaced by given replacement Source Edit

  1. func split(pattern: cstring; self: RegExp): seq[cstring] {.
  2. importjs: "(#.split(#) || [])", ...raises: [], tags: [], forbids: [].}

Divides a string into an ordered list of substrings and returns the array Source Edit

  1. func startsWith(pattern: cstring; self: RegExp): bool {....raises: [], tags: [],
  2. forbids: [].}

Tests if string starts with given RegExp

Example:

  1. let jsregex: RegExp = newRegExp(r"abc", r"i")
  2. assert "abcd".startsWith jsregex

Source Edit

  1. func toCstring(self: RegExp): cstring {.importjs: "#.toString()", ...raises: [],
  2. tags: [], forbids: [].}

Returns a string representing the RegExp object. Source Edit