std/jsre (original) (raw)

Source Edit

Regular Expressions for the JavaScript target.

Example:

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" jsregex.compile(r"([a-z])\1*", r"g") assert "abbcccdddd".replace(jsregex, proc (m: varargs[cstring]): cstring = ($m[0] & $(m.len)).cstring) == "a1b2c3d4" let digitsRegex: RegExp = newRegExp(r"\d") assert "foo".match(digitsRegex) == @[]

Procs

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

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

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

Tests for a substring match in its string parameter.

Example:

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 endsWith(pattern: cstring; self: RegExp): bool {....raises: [], tags: [], forbids: [].}

Tests if string ends with given RegExp

Example:

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

Source Edit

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

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

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

Returns an array of matches of a RegExp against given stringSource Edit

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

Creates a new RegExp object.Source Edit

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

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

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

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

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

Tests if string starts with given RegExp

Example:

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

Source Edit

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

Returns a string representing the RegExp object.Source Edit