Source   Edit

Regular Expressions for the JavaScript target.


import std/jsre
let jsregex: RegExp = newRegExp(r"\s+", r"i")
jsregex.compile(r"\w+", r"i")
assert "nim javascript".contains jsregex
assert jsregex.exec(r"nim javascript") == @["nim".cstring]
assert jsregex.toCstring() == r"/\w+/i"
jsregex.compile(r"[0-9]", r"i")
assert "0123456789abcd".contains jsregex
assert $jsregex == "/[0-9]/i"
jsregex.compile(r"abc", r"i")
assert "abcd".startsWith jsregex
assert "dabc".endsWith jsregex
jsregex.compile(r"\d", r"i")
assert "do1ne".split(jsregex) == @["do".cstring, "ne".cstring]
jsregex.compile(r"[lw]", r"i")
assert "hello world".replace(jsregex,"X") == "heXlo world"


RegExp = ref object of JsRoot
  flags*: cstring            ## cstring that contains the flags of the RegExp object.
  dotAll*: bool              ## Whether `.` matches newlines or not.
  global*: bool              ## Whether to test against all possible matches in a string, or only against the first.
  ignoreCase*: bool          ## Whether to ignore case while attempting a match in a string.
  multiline*: bool           ## Whether to search in strings across multiple lines.
  source*: cstring           ## The text of the pattern.
  sticky*: bool              ## Whether the search is sticky.
  unicode*: bool             ## Whether Unicode features are enabled.
  lastIndex*: cint           ## Index at which to start the next match (read/write property).
  input*: cstring            ## Read-only and modified on successful match.
  lastMatch*: cstring        ## Ditto.
  lastParen*: cstring        ## Ditto.
  leftContext*: cstring      ## Ditto.
  rightContext*: cstring     ## Ditto.
Regular Expressions for JavaScript target. See   Source   Edit


func newRegExp(pattern: cstring; flags: cstring): RegExp {.
    importjs: "new RegExp(@)".}
Creates a new RegExp object.   Source   Edit
func newRegExp(pattern: cstring): RegExp {.importjs: "new RegExp(@)".}
  Source   Edit
func compile(self: RegExp; pattern: cstring; flags: cstring) {.
    importjs: "#.compile(@)".}
Recompiles a regular expression during execution of a script.   Source   Edit
func replace(pattern: cstring; self: RegExp; replacement: cstring): cstring {.
    importjs: "#.replace(#, #)".}
Returns a new string with some or all matches of a pattern replaced by given replacement   Source   Edit
func split(pattern: cstring; self: RegExp): seq[cstring] {.
    importjs: "#.split(#)".}
Divides a string into an ordered list of substrings and returns the array   Source   Edit
func match(pattern: cstring; self: RegExp): seq[cstring] {.
    importjs: "#.match(#)".}
Returns an array of matches of a RegExp against given string   Source   Edit
func exec(self: RegExp; pattern: cstring): seq[cstring] {.importjs: "#.exec(#)".}
Executes a search for a match in its string parameter.   Source   Edit
func toCstring(self: RegExp): cstring {.importjs: "#.toString()".}
Returns a string representing the RegExp object.   Source   Edit
func `$`(self: RegExp): string {....raises: [], tags: [].}
  Source   Edit
func test(self: RegExp; pattern: cstring): bool {.importjs: "#.test(#)",
    deprecated: "Use contains instead".}
Deprecated: Use contains instead
  Source   Edit
func toString(self: RegExp): cstring {.importjs: "#.toString()",
                                       deprecated: "Use toCstring instead".}
Deprecated: Use toCstring instead
  Source   Edit
func contains(pattern: cstring; self: RegExp): bool {....raises: [], tags: [].}
Tests for a substring match in its string parameter.


let jsregex: RegExp = newRegExp(r"bc$", r"i")
assert jsregex in r"abc"
assert jsregex notin r"abcd"
assert "xabc".contains jsregex
  Source   Edit
func startsWith(pattern: cstring; self: RegExp): bool {....raises: [], tags: [].}
Tests if string starts with given RegExp


let jsregex: RegExp = newRegExp(r"abc", r"i")
assert "abcd".startsWith jsregex
  Source   Edit
func endsWith(pattern: cstring; self: RegExp): bool {....raises: [], tags: [].}
Tests if string ends with given RegExp


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