Expose some JS regexp functions to Lua
parent
3344845d1f
commit
47ac8cae09
|
@ -58,16 +58,15 @@ export const stringApi = new LuaTable({
|
||||||
return String.fromCharCode(...args);
|
return String.fromCharCode(...args);
|
||||||
}),
|
}),
|
||||||
find: new LuaBuiltinFunction(
|
find: new LuaBuiltinFunction(
|
||||||
(_sf, s: string, pattern: string, init?: number, plain?: boolean) => {
|
(_sf, s: string, pattern: string, init?: number) => {
|
||||||
init = init ?? 1;
|
init = init ?? 1;
|
||||||
plain = plain ?? false;
|
|
||||||
const result = s.slice(init - 1).match(pattern);
|
const result = s.slice(init - 1).match(pattern);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return new LuaMultiRes([]);
|
return new LuaMultiRes([]);
|
||||||
}
|
}
|
||||||
return new LuaMultiRes([
|
return new LuaMultiRes([
|
||||||
result.index! + 1,
|
result.index! + init,
|
||||||
result.index! + result[0].length,
|
result.index! + init + result[0].length,
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
@ -218,9 +217,15 @@ export const stringApi = new LuaTable({
|
||||||
const regex = new RegExp(pattern);
|
const regex = new RegExp(pattern);
|
||||||
const result = s.match(regex);
|
const result = s.match(regex);
|
||||||
return jsToLuaValue(result);
|
return jsToLuaValue(result);
|
||||||
// if (!result) {
|
}),
|
||||||
// return new LuaMultiRes([]);
|
match_regex_all: new LuaBuiltinFunction((_sf, s: string, pattern: string) => {
|
||||||
// }
|
const regex = new RegExp(pattern, "g");
|
||||||
// return new LuaMultiRes(result.slice(1));
|
return () => {
|
||||||
|
const match = regex.exec(s);
|
||||||
|
if (!match) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return jsToLuaValue(match);
|
||||||
|
};
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
|
@ -141,3 +141,12 @@ assert_equal(year, "14")
|
||||||
local word = string.match("The quick brown fox", "%s*(%w+)%s*")
|
local word = string.match("The quick brown fox", "%s*(%w+)%s*")
|
||||||
assert_equal(word, "The")
|
assert_equal(word, "The")
|
||||||
|
|
||||||
|
-- Test match_regex_all
|
||||||
|
local matches = {}
|
||||||
|
for match in string.match_regex_all("hellolllbl", "(l+)") do
|
||||||
|
table.insert(matches, match)
|
||||||
|
end
|
||||||
|
assert_equal(#matches, 3)
|
||||||
|
assert_equal(matches[1][1], "ll")
|
||||||
|
assert_equal(matches[2][1], "lll")
|
||||||
|
assert_equal(matches[3][1], "l")
|
||||||
|
|
Loading…
Reference in New Issue