blob: 7d51db6463d6b502c4cd1b58339f5aaa27a5374b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
## Regular Expressions for the JavaScript target.
## * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
when not defined(js):
{.error: "This module only works on the JavaScript platform".}
type RegExp* = ref object of JsRoot
## Regular Expressions for JavaScript target.
## See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
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.
func newRegExp*(pattern: cstring; flags: cstring): RegExp {.importjs: "new RegExp(@)".}
## Creates a new RegExp object.
func newRegExp*(pattern: cstring): RegExp {.importjs: "new RegExp(@)".}
func compile*(self: RegExp; pattern: cstring; flags: cstring) {.importjs: "#.compile(@)".}
## Recompiles a regular expression during execution of a script.
func exec*(self: RegExp; pattern: cstring): seq[cstring] {.importjs: "#.exec(#)".}
## Executes a search for a match in its string parameter.
func toCstring*(self: RegExp): cstring {.importjs: "#.toString()".}
## Returns a string representing the RegExp object.
func `$`*(self: RegExp): string = $toCstring(self)
func test*(self: RegExp; pattern: cstring): bool {.importjs: "#.test(#)", deprecated: "Use contains instead".}
func toString*(self: RegExp): cstring {.importjs: "#.toString()", deprecated: "Use toCstring instead".}
func contains*(pattern: cstring; self: RegExp): bool =
## Tests for a substring match in its string parameter.
runnableExamples:
let jsregex: RegExp = newRegExp(r"bc$", r"i")
assert jsregex in r"abc"
assert jsregex notin r"abcd"
assert "xabc".contains jsregex
asm "`result` = `self`.test(`pattern`);"
runnableExamples:
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"
|