2
2
3
3
local jsregexp = {}
4
4
5
+ ---
5
6
--- Compile a regular expression. Throws if an error occurs.
7
+ ---
8
+ --- Example:
6
9
--- ```lua
7
10
--- local re = jsregexp.compile("\\w+", "g")
8
11
--- ```
12
+ ---
9
13
--- @param re string
10
14
--- @param flags ? string
11
15
--- @return JSRegExp.RegExp RegExp
12
16
function jsregexp .compile (re , flags ) end
13
17
18
+ ---
14
19
--- Safely compile a regular expression.
20
+ ---
21
+ --- Example:
15
22
--- ```lua
16
23
--- local re, err = jsregexp.compile_safe("\\w+", "g")
17
24
--- if not re then
18
25
--- print(err)
19
26
--- end
20
27
--- ```
28
+ ---
21
29
--- @param re string
22
30
--- @param flags ? string
23
31
--- @return JSRegExp.RegExp ? RegExp
24
32
--- @return string ? Error
25
33
function jsregexp .compile_safe (re , flags ) end
26
34
35
+ ---
27
36
--- Convert a lua utf8 lua string to a utf16 js string. For internal use.
37
+ ---
28
38
--- @param str string
29
39
--- @return JSRegExp.JSString
30
40
function jsregexp .to_jsstring (str ) end
31
41
42
+ ---
32
43
--- UTF-16 representation of a string. For internal use.
44
+ ---
33
45
--- @class JSRegExp.JSString
34
46
47
+ ---
35
48
--- A compiled JavaScript regular expression object.
49
+ ---
36
50
--- @class JSRegExp.RegExp
37
51
--- @field last_index integer the position at wchich the next match will be searched in re : exec or re : test (see notes below )
38
52
--- @field source string the regexp string
@@ -46,30 +60,41 @@ function jsregexp.to_jsstring(str) end
46
60
--- @field unicode boolean is the unicode flag set ?
47
61
local re = {}
48
62
63
+ ---
49
64
--- Execute the regular expression against a string.
65
+ ---
66
+ --- Example:
50
67
--- ```lua
51
68
--- local re = jsregexp.compile("\\w+", "g")
52
69
--- local match = re:exec("Hello World")
53
70
--- if match then
54
71
--- print(match) -- Hello
55
72
--- end
56
73
--- ```
74
+ ---
57
75
--- @param string string | JSRegExp.JSString
58
76
--- @return JSRegExp.Match ?
59
77
function re :exec (string ) end
60
78
79
+ ---
61
80
--- Test the regular expression against a string.
81
+ ---
82
+ --- Example:
62
83
--- ```lua
63
84
--- local re = jsregexp.compile("\\w+", "g")
64
85
--- if re:test("Hello World") then
65
86
--- print("Matched!")
66
87
--- end
67
88
--- ```
89
+ ---
68
90
--- @param string string | JSRegExp.JSString
69
91
--- @return boolean
70
92
function re :test (string ) end
71
93
94
+ ---
72
95
--- Returns a list of all matches or nil if no match.
96
+ ---
97
+ --- Example:
73
98
--- ```lua
74
99
--- local re = jsregexp.compile("\\w+", "g")
75
100
--- local matches = re:match("Hello World")
@@ -79,63 +104,84 @@ function re:test(string) end
79
104
--- end
80
105
--- end
81
106
--- ```
107
+ ---
82
108
--- @param string string
83
109
--- @return JSRegExp.Match[] ?
84
110
function re :match (string ) end
85
111
112
+ ---
86
113
--- Returns a closure that repeatedly calls re:exec, to be used in for-loops.
114
+ ---
115
+ --- Example:
87
116
--- ```lua
88
117
--- local re = jsregexp.compile("\\w+", "g")
89
118
--- for match in re:match_all("Hello World") do
90
119
--- print(match)
91
120
--- end
92
121
--- ```
122
+ ---
93
123
--- @param string string
94
124
--- @return fun (): JSRegExp.Match
95
125
function re :match_all (string ) end
96
126
127
+ ---
97
128
--- Returns a list of all matches.
129
+ ---
130
+ --- Example:
98
131
--- ```lua
99
132
--- local re = jsregexp.compile("\\w+", "g")
100
133
--- local matches = re:match("Hello World")
101
134
--- for _, match in ipairs(matches) do
102
135
--- print(match)
103
136
--- end
104
137
--- ```
138
+ ---
105
139
--- @param string string
106
140
--- @return JSRegExp.Match[]
107
141
function re :match_all_list (string ) end
108
142
143
+ ---
109
144
--- Returns the 1-based index of the first match of re in str, or -1 if no match
145
+ ---
146
+ --- Example:
110
147
--- ```lua
111
148
--- local re = jsregexp.compile("Wo\\w+")
112
149
--- local idx = re:match("Hello World")
113
150
--- if idx > 0 then
114
151
--- print("Matched at index " .. idx)
115
152
--- end
116
153
--- ```
154
+ ---
117
155
--- @param string string
118
156
--- @return integer
119
157
function re :search (string ) end
120
158
159
+ ---
121
160
--- Splits str at re, at most `limit` times
161
+ ---
162
+ --- Example:
122
163
--- ```lua
123
164
--- local re = jsregexp.compile("\\s+")
124
165
--- local res = re:split("Hello World")
125
166
--- for _, str in ipairs(res) do
126
167
--- print(str)
127
168
--- end
128
169
--- ```
170
+ ---
129
171
--- @param string string
130
172
--- @param limit ? integer
131
173
--- @return string[]
132
174
function re :split (string , limit ) end
133
175
176
+ ---
134
177
--- Relplace the first match (all matches, if global) of re in str by replacement.
178
+ ---
179
+ --- Example:
135
180
--- ```lua
136
181
--- local re = jsregexp.compile("\\w+")
137
182
--- local res = re:replace("Hello World", "Hey")
138
183
--- print(res) -- Hey World
184
+ ---
139
185
--- local res2 = re:replace("Hello World", function(match, str)
140
186
--- if match[0] == "Hello" then
141
187
--- return "Hey"
@@ -147,16 +193,21 @@ function re:split(string, limit) end
147
193
--- end)
148
194
--- print(res2) -- Hey World
149
195
--- ```
196
+ ---
150
197
--- @param string string
151
198
--- @param replacement (fun ( match : JSRegExp.Match , str : string ): string )| string
152
199
--- @return string
153
200
function re :replace (string , replacement ) end
154
201
202
+ ---
155
203
--- Relplace all occurances of re in str by replacement.
204
+ ---
205
+ --- Example:
156
206
--- ```lua
157
207
--- local re = jsregexp.compile("\\w+", "g")
158
208
--- local res = re:replace_all("Hello World", "Hey")
159
209
--- print(res) -- Hey Hey
210
+ ---
160
211
--- local res2 = re:replace_all("Hello World", function(match, str)
161
212
--- if match[0] == "Hello" then
162
213
--- return "Hey"
@@ -168,6 +219,7 @@ function re:replace(string, replacement) end
168
219
--- end)
169
220
--- print(res2) -- Hey Joe
170
221
--- ```
222
+ ---
171
223
--- @param string string
172
224
--- @param replacement (fun ( match : JSRegExp.Match , str : string ): string )| string
173
225
--- @return string
0 commit comments