kmarius / jsregexp Goto Github PK
View Code? Open in Web Editor NEWJavaScript regular expressions for Lua
License: MIT License
JavaScript regular expressions for Lua
License: MIT License
For both python and javascript the standard behavior is to raise an error when compilation fails. I think it would be good if the default is the same for jsregexp.compile()
. A separate jsregexp.compile_safe()
function can be created that prevents error raising. This is a common place practice for other such projects like cjson.
Pinging @L3MON4D3
I would like to remove the old, undocumented __call
metamethod at some point in the future because it duplicates a lot of the matching logic.
As far as I can tell, LuaSnip only uses the begin_ind
property and the capture groups of a match. A simple migration step could look like this: when loading jsregexp, check if the __call
metamethod exists, and if not set to something like this:
local jsregexp = require("jsregexp")
local meta = getmetatable(jsregexp.compile(""))
meta.__call = function(self, str)
local match = self:exec(str)
if not match then
return {}
end
return {
{
begin_ind = match.index,
groups = { unpack(match) },
},
}
end
This should work as long as only the first match is needed and the global flag is not set. A full replacement looks more like this (Note that the match object already contains the full match in match[0]
which could be used directly by LuaSnip):
meta.__call = function(self, s)
local jstr = jsregexp.to_jsstring(s)
self.last_index = 1
local matches = {}
while true do
local match = self:exec(jstr)
if match == nil then
break
end
table.insert(matches, {
begin_ind = match.index,
end_ind = match.index + #match[0] - 1,
groups = { unpack(match) },
named_groups = match.groups,
})
if not self.global then
break
end
if #match[0] == 0 then
self.last_index = self.last_index + 1
end
end
return matches
end
Opinions? I am not planning to release anything until I have the OK, and there is no urgency whatsoever.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.