package
A source code transpiler that enables the use of ES2015 Unicode regular expressions in ES5.
npm install regexpuGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
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.
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, , , and let you try regexps as well as other ES.next features.
Consider a file named example-es2015.js with the following contents:
var string = 'foo💩bar';
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.
Let’s transpile it:
$ regexpu < example-es2015.js > example-es5.jsexample-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\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
RegExp('…', 'u') are not affected.RegExp.prototype.unicode getter because it’s not possible to do so without side effects.i and u flag set, since that would require transpiling/wrapping strings.To use regexpu programmatically, install it as a dependency via npm:
npm install regexpu --save-devTo use the command-line interface, install regexpu globally:
npm install regexpu -gregexpu.versionA 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 project’s documentation for more information.
regexpu.rewritePattern uses regjsgen, regjsparser, and regenerate as internal dependencies. If you only need this function in your program, it’s 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 mapThe 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, it’s 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("foo💩bar"));';
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("foo💩bar"));'The optional options object recognizes the following properties:
sourceFileName: a string representing the file name of the original ES2015 source file.sourceMapName: a string representing the desired file name of the source map.dotAllFlag: a boolean indicating whether to enable experimental support for the s (dotAll) flag.unicodePropertyEscape: a boolean indicating whether to enable experimental support for Unicode property escapes.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 mapregexpu.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');If you’re looking for a general-purpose ES.next-to-ES5 transpiler with support for Unicode regular expressions, consider using one of these:
On the main branch, bump the version number in package.json:
npm version patch -m 'Release v%s'Instead of patch, use minor or major as needed.
Note that this produces a Git commit + tag.
Push the release commit and tag:
git push && git push --tagsOur CI then automatically publishes the new release to npm.
regexpu is available under the MIT license.
jridgewell
justin+npm@ridgewell.name
uEstimated unpacked size across 17 packages.
2.5 MB
Includes dependencies.
3 direct · 2 outdated