Git Product home page Git Product logo

jsregexp's Introduction

jsregexp

Provides (a subset of) ECMAScript regular expressions. Uses libregexp from Fabrice Bellard's QuickJS.

Installation

If you intend to use jsregexp in neovim you can place

use_rocks 'jsregexp'

in the function passed to packer.startup.

To install jsregexp globally with luarocks, run

sudo luarocks --lua-version 5.1 install jsregexp

To install jsregexp locally, run

luarocks --local --lua-version 5.1 install jsregexp

This will place the compiled module in $HOME/.luarocks/lib/lua/5.1 so $HOME/.luarocks/lib/lua/5.1/?.so needs to be added to package.cpath.

Running make in this project's root will compile the module jsregexp.so and place it in the same directory.

Usage

This module provides two functions

jsregexp.compile(regex, flags?)
jsregexp.compile_safe(regex, flags?)

that take an ECMAScript regular expression as a string and an optional string of flags:

  • "i": case insensitive search
  • "g": match globally
  • "n": named groups
  • "u": implicitly set utf-16 support if detected in the pattern string

On success, compile returns a function that takes a string as its single argument and returns a table containing all matches. On failure, compile throws an error while compile_save returns nil and an error message.

The function returns an empty table if the regular expression does not match. It will always return a table of matches even if the global flag "g" is not set and only a single match (at most) is possible.

Each match consists of a table with the fields

match.begin_ind  -- begin of the match
match.end_ind    -- end of the match, points at the last character (or byte, if non-ascii)
match.groups     -- a table containing the strings of the match group corresponding to the index

Example

local jsregexp = require("jsregexp")

local regex, err = jsregexp.compile_safe("(\\w)\\w*", "g")
if not regex then
	print(err)
	return
end

local str = "Hello World"
local matches = regex(str)

for i, match in ipairs(matches) do
	print(string.format("match %d: %s", i, str:sub(match.begin_ind, match.end_ind)))
	if #match.groups > 0 then
		print("capture groups:")
		for j, group in ipairs(match.groups) do
			print(j, group)
		end
	end
end

jsregexp's People

Contributors

kmarius avatar l3mon4d3 avatar nathanpage-credo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.