What is re2?
The re2 npm package is a wrapper around Google's RE2 regular expression library, which is designed to be fast and safe. It provides a way to perform regular expression operations without the risk of catastrophic backtracking, which can occur with some other regular expression engines.
What are re2's main functionalities?
Basic Matching
This feature allows you to perform basic matching operations using regular expressions. The code sample demonstrates how to create a new RE2 instance with a pattern and test a string against it.
const RE2 = require('re2');
const re = new RE2('hello');
console.log(re.test('hello world')); // true
Capturing Groups
This feature allows you to use capturing groups in your regular expressions. The code sample shows how to capture groups of digits separated by a hyphen and access the captured groups.
const RE2 = require('re2');
const re = new RE2('(\d+)-(\d+)');
const match = re.exec('123-456');
console.log(match[1]); // 123
console.log(match[2]); // 456
Global Matching
This feature allows you to perform global matching to find all occurrences of a pattern in a string. The code sample demonstrates how to find all sequences of digits in a string.
const RE2 = require('re2');
const re = new RE2('\d+', 'g');
const matches = '123 456 789'.match(re);
console.log(matches); // ['123', '456', '789']
Replacing
This feature allows you to replace parts of a string that match a pattern with a replacement string. The code sample shows how to replace 'world' with 'RE2' in a given string.
const RE2 = require('re2');
const re = new RE2('world');
const result = 'hello world'.replace(re, 'RE2');
console.log(result); // 'hello RE2'
Other packages similar to re2
regexp
The 'regexp' package provides a simple interface for working with regular expressions in JavaScript. It is similar to re2 but does not offer the same level of performance and safety guarantees against catastrophic backtracking.
xregexp
The 'xregexp' package extends JavaScript's native RegExp with additional features and syntax. It offers more functionality than re2 but may not be as performant or safe in terms of avoiding catastrophic backtracking.
pcre-to-regexp
The 'pcre-to-regexp' package allows you to convert Perl-compatible regular expressions (PCRE) to JavaScript RegExp objects. While it provides compatibility with PCRE syntax, it does not offer the same performance and safety benefits as re2.
node-re2
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);
var re2 = new RE2(re1);
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");
var re = new RE2("a(b*)");
var result = re.exec("abbc");
console.log(result[0]);
console.log(result[1]);
result = re.exec("aBbC");
console.log(result[0]);
console.log(result[1]);
re = new RE2("a(b*)", "i");
result = re.exec("aBbC");
console.log(result[0]);
console.log(result[1]);
var regexp = new RegExp("a(b*)", "i");
re = new RE2(regexp);
result = re.exec("aBbC");
console.log(result[0]);
console.log(result[1]);
re = new RE2(/a(b*)/i);
result = re.exec("aBbC");
console.log(result[0]);
console.log(result[1]);
var rex = new RE2(re);
result = rex.exec("aBbC");
console.log(result[0]);
console.log(result[1]);
result = new RE2("ab*").exec("abba");
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");
/(cat|dog)\1/.test("dogdog");
/(cat|dog)\1/.test("catdog");
/(cat|dog)\1/.test("dogcat");
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