All Projects → mathiasbynens → Regexpu

mathiasbynens / Regexpu

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

Programming Languages

javascript
184084 projects - #8 most used programming language
es2015
71 projects
ecmascript
72 projects

Projects that are alternatives of or similar to Regexpu

Emoji Regex
A regular expression to match all Emoji-only symbols as per the Unicode Standard.
Stars: ✭ 1,134 (+464.18%)
Mutual labels:  regex, unicode, regular-expression, regexp
Regenerate
Generate JavaScript-compatible regular expressions based on a given set of Unicode symbols or code points.
Stars: ✭ 306 (+52.24%)
Mutual labels:  code-generation, unicode, regexp
Orchestra
One language to be RegExp's Successor. Visually readable and rich, technically safe and extended, naturally scalable, advanced, and optimized
Stars: ✭ 103 (-48.76%)
Mutual labels:  regex, regular-expression, regexp
Hyperscan Java
Match tens of thousands of regular expressions within milliseconds - Java bindings for Intel's hyperscan 5
Stars: ✭ 66 (-67.16%)
Mutual labels:  regex, regular-expression, regexp
stringx
Drop-in replacements for base R string functions powered by stringi
Stars: ✭ 14 (-93.03%)
Mutual labels:  unicode, regex, regexp
Rex
Your RegEx companion.
Stars: ✭ 283 (+40.8%)
Mutual labels:  regex, regular-expression, regexp
Picomatch
Blazing fast and accurate glob matcher written JavaScript, with no dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions.
Stars: ✭ 393 (+95.52%)
Mutual labels:  regex, regular-expression, regexp
regexp-expand
Show the ELisp regular expression at point in rx form.
Stars: ✭ 18 (-91.04%)
Mutual labels:  regex, regexp, regular-expression
Onigmo
Onigmo is a regular expressions library forked from Oniguruma.
Stars: ✭ 536 (+166.67%)
Mutual labels:  regex, regular-expression, regexp
Commonregex
🍫 A collection of common regular expressions for Go
Stars: ✭ 733 (+264.68%)
Mutual labels:  regex, regular-expression, regexp
Regex Dos
👮 👊 RegEx Denial of Service (ReDos) Scanner
Stars: ✭ 143 (-28.86%)
Mutual labels:  regex, regular-expression, regexp
RgxGen
Regex: generate matching and non matching strings based on regex pattern.
Stars: ✭ 45 (-77.61%)
Mutual labels:  regex, regexp, regular-expression
globrex
Glob to regular expression with support for extended globs.
Stars: ✭ 52 (-74.13%)
Mutual labels:  regex, regexp, regular-expression
Proposal Regexp Unicode Property Escapes
Proposal to add Unicode property escapes `\p{…}` and `\P{…}` to regular expressions in ECMAScript.
Stars: ✭ 112 (-44.28%)
Mutual labels:  regex, unicode, regexp
expand-brackets
Expand POSIX bracket expressions (character classes) in glob patterns.
Stars: ✭ 26 (-87.06%)
Mutual labels:  regex, regexp, regular-expression
Regexp2
A full-featured regex engine in pure Go based on the .NET engine
Stars: ✭ 389 (+93.53%)
Mutual labels:  regex, regular-expression, regexp
Regaxor
A regular expression fuzzer.
Stars: ✭ 35 (-82.59%)
Mutual labels:  regex, regexp, regular-expression
cregex
A small implementation of regular expression matching engine in C
Stars: ✭ 72 (-64.18%)
Mutual labels:  regex, regexp, regular-expression
Regulex
🚧 Regular Expression Excited!
Stars: ✭ 4,877 (+2326.37%)
Mutual labels:  regex, regular-expression, regexp
Regexr
For composing regular expressions without the need for double-escaping inside strings.
Stars: ✭ 53 (-73.63%)
Mutual labels:  regex, regular-expression, regexp

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.

Here’s 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 = '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.test('💫'), // true
  regex.test('💬')  // false
]);

Let’s 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 = '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-\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 doesn’t polyfill the RegExp.prototype.unicode getter because it’s not possible to do so without side effects.
  3. regexpu doesn’t 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 doesn’t 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 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 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, 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:

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 you’re 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].