Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

regexpu

A source code transpiler that enables the use of ES2015 Unicode regular expressions in ES5.

Subscribe to updates I use regexpu


Statistics on regexpu

Number of watchers on Github 126
Number of open issues 23
Average time to close an issue 27 days
Main language JavaScript
Average time to merge a PR about 1 hour
Open pull requests 30+
Closed pull requests 15+
Last commit over 2 years ago
Repo Created over 5 years ago
Repo Last Updated over 2 years ago
Size 201 KB
Homepage https://mths.be/r...
Organization / Authormathiasbynens
Latest Releasev2.0.0
Contributors4
Page Updated
Do you use regexpu? Leave a review!
View open issues (23)
View regexpu activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating regexpu for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

regexpu Build status Code coverage status

regexpu is a source code transpiler that enables the use of ES2015 Unicode regular expressions in JavaScript-of-today (ES5). It rewrites regular expressions that make use of the ES2015 u flag into equivalent ES5-compatible regular expressions.

Heres an online demo.

Traceur v0.0.61+, Babel v1.5.0+, esnext v0.12.0+, and Bubl v0.12.0+ use regexpu for their u regexp transpilation. The REPL demos for Traceur, Babel, esnext, and Bubl let you try u regexps as well as other ES.next features.

Example

Consider a file named example-es2015.js with the following contents:

var string = 'foobar';
var match = string.match(/foo(.)bar/u);
console.log(match[1]);
//  ''

// This regex matches any symbol from U+1F4A9 to U+1F4AB, and nothing else.
var regex = /[\u{1F4A9}-\u{1F4AB}]/u;
// The following regex is equivalent.
var alternative = /[-]/u;
console.log([
  regex.test('a'),  // false
  regex.test(''), // true
  regex.test(''), // true
  regex.test(''), // true
  regex.test('')  // false
]);

Lets transpile it:

$ regexpu < example-es2015.js > example-es5.js

example-es5.js can now be used in ES5 environments. Its contents are as follows:

var string = 'foobar';
var match = string.match(/foo((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))bar/);
console.log(match[1]);
//  ''

// This regex matches any symbol from U+1F4A9 to U+1F4AB, and nothing else.
var regex = /(?:\uD83D[\uDCA9-\uDCAB])/;
// The following regex is equivalent.
var alternative = /(?:\uD83D[\uDCA9-\uDCAB])/;
console.log([
  regex.test('a'),  // false
  regex.test(''), // true
  regex.test(''), // true
  regex.test(''), // true
  regex.test('')  // false
]);

Known limitations

  1. regexpu only transpiles regular expression literals, so things like RegExp('', 'u') are not affected.
  2. regexpu doesnt polyfill the RegExp.prototype.unicode getter because its not possible to do so without side effects.
  3. regexpu doesnt support canonicalizing the contents of back-references in regular expressions with both the i and u flag set, since that would require transpiling/wrapping strings.
  4. regexpu doesnt match lone low surrogates accurately. Unfortunately that is impossible to implement due to the lack of lookbehind support in JavaScript regular expressions.

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, options)

This is an alias for the rewritePattern function exported by regexpu-core. Please refer to that projects documentation for more information.

regexpu.rewritePattern uses regjsgen, regjsparser, and regenerate as internal dependencies. If you only need this function in your program, its better to include it directly:

// Instead of
const rewritePattern = require('regexpu').rewritePattern;

// Use this:
const rewritePattern = require('regexpu-core');

This prevents the Recast and Esprima dependencies from being loaded into memory.

regexpu.transformTree(ast, options) or its alias regexpu.transform(ast, options)

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 ES2015 u flag are rewritten in ES5.

const regexpu = require('regexpu');
const recast = require('recast');
const tree = recast.parse(code); // ES2015 code
const transformedTree = regexpu.transform(tree);
const result = recast.print(transformedTree);
console.log(result.code); // transpiled ES5 code
console.log(result.map); // source map

The optional options object is passed to _regexpu-core_s rewritePattern. For a description of the available options, see its documentation.

regexpu.transformTree uses Recast, regjsgen, regjsparser, and regenerate as internal dependencies. If you only need this function in your program, its better to include it directly:

const transformTree = require('regexpu/transform-tree');

This prevents the Esprima dependency from being loaded into memory.

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 ES2015 u flag are rewritten in ES5.

const es2015 = 'console.log(/foo.bar/u.test("foobar"));';
const es5 = regexpu.transpileCode(es2015);
//  'console.log(/foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF])bar/.test("foobar"));'

The optional options object recognizes the following properties:

The sourceFileName and sourceMapName properties must be provided if you want to generate source maps.

const result = regexpu.transpileCode(code, {
  'sourceFileName': 'es2015.js',
  'sourceMapName': 'es2015.js.map',
});
console.log(result.code); // transpiled source code
console.log(result.map); // source map

regexpu.transpileCode uses Esprima, Recast, regjsgen, regjsparser, and regenerate as internal dependencies. If you only need this function in your program, feel free to include it directly:

const transpileCode = require('regexpu/transpile-code');

Transpilers that use regexpu internally

If youre looking for a general-purpose ES.next-to-ES5 transpiler with support for Unicode regular expressions, consider using one of these:

Author

twitter/mathias
Mathias Bynens

License

regexpu is available under the MIT license.

regexpu open issues Ask a question     (View All Issues)
  • over 3 years Missing module jsesc
  • over 5 years Integration with ES6 to ES5 transpilers
regexpu open pull requests (View All Pulls)
  • Update jsesc to version 1.0.0 ?
  • Update jsesc to version 2.1.0 ?
  • Update jsesc to version 2.0.0 ?
  • Update jsesc to version 1.3.0 ?
  • Update jsesc to version 1.2.1 ?
  • mocha@2.5.0 breaks build ?
  • Update jsesc to version 1.2.0 ?
  • Update jsesc to version 1.1.0 ?
  • regexpu-fixtures@2.1.1 breaks build 🚨
  • Update mocha to version 3.0.2 🚀
  • Update mocha to version 3.0.1 🚀
  • Update mocha to version 3.0.0 🚀
  • Update recast to version 0.12.3 🚀
  • Update browserify to version 14.3.0 🚀
  • Update browserify to version 14.2.0 🚀
  • Update recast to version 0.12.2 🚀
  • Update recast to version 0.12.1 🚀
  • Update codecov to version 2.1.0 🚀
  • Update codecov to version 2.0.2 🚀
  • Update codecov to version 2.0.1 🚀
  • Update recast to version 0.12.0 🚀
  • Update babili to version 0.0.12 🚀
  • Update browserify to version 14.1.0 🚀
  • Update babili to version 0.0.11 🚀
  • Update browserify to version 14.0.0 🚀
  • Update babili to version 0.0.10 🚀
  • Update babili to version 0.1.2 🚀
  • Update babili to version 0.1.1 🚀
  • Update babili to version 0.1.0 🚀
  • mocha@3.4.0 breaks build 🚨
regexpu list of languages used
regexpu latest release notes
v2.0.0 v2.0.0

Breaking change: require('regexpu-core') is the new require('regexpu/rewrite-pattern').

Other projects in JavaScript