What is regexpu?
The regexpu package is a tool that transforms regular expressions written in ES6/ES2015+ syntax into equivalent ES5-compatible code. This is particularly useful for developers who want to use modern JavaScript features while maintaining compatibility with older environments.
What are regexpu's main functionalities?
Transform Unicode Regular Expressions
This feature allows you to transform regular expressions that use the 'u' flag for Unicode support into equivalent ES5-compatible code. The code sample demonstrates how to use regexpu to transform a simple Unicode regular expression.
const regexpu = require('regexpu-core');
const output = regexpu('foo', 'u', { unicode: true });
console.log(output); // Output: 'foo'
Transform Sticky Regular Expressions
This feature allows you to transform regular expressions that use the 'y' flag for sticky matching into equivalent ES5-compatible code. The code sample shows how to transform a sticky regular expression.
const regexpu = require('regexpu-core');
const output = regexpu('foo', 'y', { sticky: true });
console.log(output); // Output: '(?:foo)'
Transform DotAll Regular Expressions
This feature allows you to transform regular expressions that use the 's' flag for dotAll mode into equivalent ES5-compatible code. The code sample illustrates how to transform a dotAll regular expression.
const regexpu = require('regexpu-core');
const output = regexpu('foo.bar', 's', { dotAll: true });
console.log(output); // Output: 'foo[\s\S]bar'
Other packages similar to regexpu
babel-plugin-transform-es2015-unicode-regex
This Babel plugin transforms ES2015 Unicode regular expressions into equivalent ES5 code. It is similar to regexpu in that it provides compatibility for modern JavaScript features in older environments, but it is specifically designed to be used as a Babel plugin.
regexgen
Regexgen is a tool for generating regular expressions from a list of strings. While it does not directly transform ES6 regular expressions like regexpu, it provides a way to create optimized regular expressions, which can be useful in similar contexts.
regexpu
regexpu is a source code transpiler that enables the use of ES6 Unicode regular expressions in JavaScript-of-today (ES5). It rewrites regular expressions that make use of the ES6 u
flag into equivalent ES5-compatible regular expressions.
Example
Consider a file named example-es6.js
with the following contents:
var string = 'foo💩bar';
var match = string.match(/foo(.)bar/u);
console.log(match[1]);
var regex = /[\u{1F4A9}-\u{1F4AB}]/u;
var alternative = /[💩-💫]/u;
console.log([
regex.test('a'),
regex.test('💩'),
regex.test('💪'),
regex.test('💫'),
regex.test('💬')
]);
Let’s transpile it:
$ regexpu -f example-es6.js > example-es5.js
example-es5.js
can now be used in ES5 environments. Its contents are as follows:
var string = 'foo💩bar';
var match = string.match(/foo((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF]))bar/);
console.log(match[1]);
var regex = /(?:\uD83D[\uDCA9-\uDCAB])/;
var alternative = /(?:\uD83D[\uDCA9-\uDCAB])/;
console.log([
regex.test('a'),
regex.test('💩'),
regex.test('💪'),
regex.test('💫'),
regex.test('💬')
]);
Known limitations
- regexpu only transpiles regular expression literals, so things like
RegExp('…', 'u')
are not affected. - It doesn’t polyfill the
RegExp.prototype.unicode
getter because it’s not possible to do so without side effects.
Installation
To use regexpu programmatically, install it as a dependency via npm:
npm install regexpu --save-dev
To use the command-line interface, install regexpu globally:
npm install regexpu -g
API
regexpu.version
A string representing the semantic version number.
regexpu.rewritePattern(pattern, flags)
This function takes a string that represents a regular expression pattern as well as a string representing its flags, and returns an ES5-compatible version of the pattern.
regexpu.rewritePattern('foo.bar', 'u');
regexpu.rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'u');
regexpu.rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'ui');
regexpu can rewrite non-ES6 regular expressions too, which is useful to demonstrate how their behavior changes once the u
and i
flags are added:
regexpu.rewritePattern('foo.bar');
regexpu.rewritePattern('foo.bar', 'u');
regexpu.transformTree(ast)
This function accepts an abstract syntax tree representing some JavaScript code, and returns a transformed version of the tree in which any regular expression literals that use the ES6 u
flag are rewritten in ES5.
var regexpu = require('regexpu');
var recast = require('recast');
var tree = recast.parse(code);
tree = regexpu.transform(tree);
var result = recast.print(tree);
console.log(result.code);
console.log(result.map);
regexpu.transpileCode(code, options)
This function accepts a string representing some JavaScript code, and returns a transpiled version of this code tree in which any regular expression literals that use the ES6 u
flag are rewritten in ES5.
var es6 = 'console.log(/foo.bar/u.test("foo💩bar"));';
var es5 = regexpu.transpileCode(es6);
The optional options
object recognizes the following properties:
sourceFileName
: a string representing the file name of the original ES6 source file.sourceMapName
: a string representing the desired file name of the source map.
These properties must be provided if you want to generate source maps.
var result = regexpu.transpileCode(code, {
'sourceFileName': 'es6.js',
'sourceMapName': 'es6.map',
});
console.log(result.code);
console.log(result.map);
Author
License
regexpu is available under the MIT license.