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
|
The `match()` function applies the given regular expression pattern on
the given subject value.
Depending on whether the given regular expression sets the global (`g`)
modifier, either an array of match groups or the first match group is
returned.
Returns `null` if the given pattern argument is not a regular expression
value, or if the subject is `null` or unspecified.
-- Testcase --
{%
print(join("\n", [
// match all key=value pairs
match("kind=fruit name=strawberry color=red", /([[:alpha:]]+)=([^= ]+)/g),
// match any word
match("The quick brown fox jumps over the lazy dog", /[[:alpha:]]+/g),
// match the first three lowercase words
match("The quick brown fox jumps over the lazy dog", / ([[:lower:]]+) ([[:lower:]]+) ([[:lower:]]+)/),
// special case: match any empty string sequence
match("foo", /()/g),
// special case: match first empty string sequence
match("foo", /()/),
// subject is implictly converted to string
match(true, /u/)
]), "\n");
%}
-- End --
-- Expect stdout --
[ [ "kind=fruit", "kind", "fruit" ], [ "name=strawberry", "name", "strawberry" ], [ "color=red", "color", "red" ] ]
[ [ "The" ], [ "quick" ], [ "brown" ], [ "fox" ], [ "jumps" ], [ "over" ], [ "the" ], [ "lazy" ], [ "dog" ] ]
[ " quick brown fox", "quick", "brown", "fox" ]
[ [ "", "" ], [ "", "" ], [ "", "" ], [ "", "" ] ]
[ "", "" ]
[ "u" ]
-- End --
Omitting the subject yields `null`.
-- Testcase --
{%
printf("%.J\n", match(null, /u/));
%}
-- End --
-- Expect stdout --
null
-- End --
|