JSPM

  • Created
  • Published
  • Downloads 1020116
  • Score
    100M100P100Q221822F

node.js bindings for RE2: fast, safe alternative to backtracking regular expression engines.

Package Exports

  • re2

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (re2) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

node-re2

Build status Dependencies devDependencies NPM version

This project is node.js bindings for RE2: fast, safe alternative to backtracking regular expression engines. Its regular expression language is almost a superset of what is provided by RegExp, but it lacks one feature: backreferences. See below for more details.

RE2 object emulates standard RegExp making it a practical drop-in replacement in most cases. RE2 is extended to provide String-based regular expression methods as well. To help converting RegExp objects to RE2 its constructor can take RegExp directly honoring all properties.

Standard features

It can be created just like RegExp:

Supported properties:

Supported methods:

Extensions

The following is the list of extensions.

RE2 object can be created from a regular expression:

var re1 = new RE2(/ab*/ig); // from RegExp object
var re2 = new RE2(re1);     // from RE2 object

Standard String defines four more methods that can use regular expressions. RE2 provides as methods exchanging positions of a string, and a regular expression:

How to install

Installation:

npm install re2

How to use

var RE2 = require("re2");

// with default flags
var re = new RE2("a(b*)");
var result = re.exec("abbc");
console.log(result[0]); // "abb"
console.log(result[1]); // "bb"

result = re.exec("aBbC");
console.log(result[0]); // "a"
console.log(result[1]); // ""

// with explicit flags
re = new RE2("a(b*)", "i");
result = re.exec("aBbC");
console.log(result[0]); // "aBb"
console.log(result[1]); // "Bb"

// from regular expression object
var regexp = new RegExp("a(b*)", "i");
re = new RE2(regexp);
result = re.exec("aBbC");
console.log(result[0]); // "aBb"
console.log(result[1]); // "Bb"

// from regular expression literal
re = new RE2(/a(b*)/i);
result = re.exec("aBbC");
console.log(result[0]); // "aBb"
console.log(result[1]); // "Bb"

// from another RE2 object
var rex = new RE2(re);
result = rex.exec("aBbC");
console.log(result[0]); // "aBb"
console.log(result[1]); // "Bb"

// shortcut
result = new RE2("ab*").exec("abba");

// factory
result = RE2("ab*").exec("abba");

Backreferences

Unlike RegExp, RE2 doesn't support backreferences, which are numbered references to previously matched groups, like so: \1, \2, and so on. Example of backrefrences:

/(cat|dog)\1/.test("catcat"); // true
/(cat|dog)\1/.test("dogdog"); // true
/(cat|dog)\1/.test("catdog"); // false
/(cat|dog)\1/.test("dogcat"); // false

If your application uses this kind of matching, you should use RegExp.

Release history

  • 1.0.0 implemeted all RegExp methods, and all relevant String methods
  • 0.9.0 the initial public release