regexpu-core
regexpu’s core functionality (i.e. `rewritePattern(pattern, flag)`), capable of translating ES6 Unicode regular expressions to ES5.
Last updated 2 years ago by google-wombot .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install regexpu-core 
SYNC missed versions from official npm registry.

regexpu-core Build status regexpu-core on npm

regexpu is a source code transpiler that enables the use of ES2015 Unicode regular expressions in JavaScript-of-today (ES5).

regexpu-core contains regexpu’s core functionality, i.e. rewritePattern(pattern, flag), which enables rewriting regular expressions that make use of the ES2015 u flag into equivalent ES5-compatible regular expression patterns.

Installation

To use regexpu-core programmatically, install it as a dependency via npm:

npm install regexpu-core --save

Then, require it:

const rewritePattern = require('regexpu-core');

API

This module exports a single function named rewritePattern.

rewritePattern(pattern, flags, options)

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.

rewritePattern('foo.bar', 'u');
// → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF])bar'

rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'u');
// → '(?:[a-z]|\\uD834[\\uDF06-\\uDF08])'

rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'ui');
// → '(?:[a-z\\u017F\\u212A]|\\uD834[\\uDF06-\\uDF08])'

regexpu-core can rewrite non-ES6 regular expressions too, which is useful to demonstrate how their behavior changes once the u and i flags are added:

// In ES5, the dot operator only matches BMP symbols:
rewritePattern('foo.bar');
// → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF])bar'

// But with the ES2015 `u` flag, it matches astral symbols too:
rewritePattern('foo.bar', 'u');
// → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF])bar'

The optional options argument recognizes the following properties:

Stable regular expression features

These options can be set to false or 'transform'. When using 'transform', the corresponding features are compiled to older syntax that can run in older browsers. When using false (the default), they are not compiled and they can be relied upon to compile more modern features.

  • unicodeFlag - The u flag, enabling support for Unicode code point escapes in the form \u{...}.

    rewritePattern('\\u{ab}', '', {
      unicodeFlag: 'transform'
    });
    // → '\\u{ab}'
    
    rewritePattern('\\u{ab}', 'u', {
      unicodeFlag: 'transform'
    });
    // → '\\xAB'
    
  • dotAllFlag - The s (dotAll) flag.

    rewritePattern('.', '', {
      dotAllFlag: 'transform'
    });
    // → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]'
    
    rewritePattern('.', 's', {
      dotAllFlag: 'transform'
    });
    // → '[\\0-\\uFFFF]'
    
    rewritePattern('.', 'su', {
      dotAllFlag: 'transform'
    });
    // → '(?:[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])'
    
  • unicodePropertyEscapes - Unicode property escapes.

    By default they are compiled to Unicode code point escapes of the form \u{...}. If the unicodeFlag option is set to 'transform' they often result in larger output, although there are cases (such as \p{Lu}) where it actually decreases the output size.

    rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
      unicodePropertyEscapes: 'transform'
    });
    // → '[\\u{14400}-\\u{14646}]'
    
    rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
      unicodeFlag: 'transform',
      unicodePropertyEscapes: 'transform'
    });
    // → '(?:\\uD811[\\uDC00-\\uDE46])'
    
  • namedGroups - Named capture groups.

    rewritePattern('(?<name>.)\\k<name>', '', {
      namedGroups: 'transform'
    });
    // → '(.)\1'
    

Experimental regular expression features

These options can be set to false, 'parse' and 'transform'. When using 'transform', the corresponding features are compiled to older syntax that can run in older browsers. When using 'parse', they are parsed and left as-is in the output pattern. When using false (the default), they result in a syntax error if used.

Once these features become stable (when the proposals are accepted as part of ECMAScript), they will be parsed by default and thus 'parse' will behave like false.

  • unicodeSetsFlag - The v (unicodeSets) flag

    rewritePattern('[\\p{Emoji}&&\\p{ASCII}]', 'u', {
      unicodeSetsFlag: 'transform'
    });
    // → '[#\*0-9]'
    

    By default, patterns with the v flag are transformed to patterns with the u flag. If you want to downlevel them more you can set the unicodeFlag: 'transform' option.

    rewritePattern('[^[a-h]&&[f-z]]', 'v', {
      unicodeSetsFlag: 'transform'
    });
    // → '[^f-h]' (to be used with /u)
    
    rewritePattern('[^[a-h]&&[f-z]]', 'v', {
      unicodeSetsFlag: 'transform',
      unicodeFlag: 'transform'
    });
    // → '(?:(?![f-h])[\s\S])' (to be used without /u)
    
  • modifiers - Inline m/s/i modifiers

    rewritePattern('(?i:[a-z])[a-z]', '', {
      modifiers: 'transform'
    });
    // → '(?:[a-zA-Z])([a-z])'
    

Miscellaneous options

  • onNamedGroup

    This option is a function that gets called when a named capture group is found. It receives two parameters: the name of the group, and its index.

    rewritePattern('(?<name>.)\\k<name>', '', {
      onNamedGroup(name, index) {
        console.log(name, index);
        // → 'name', 1
      }
    });
    
  • onNewFlags

    This option is a function that gets called to pass the flags that the resulting pattern must be interpreted with.

    rewritePattern('abc', 'um', '', {
      unicodeFlag: 'transform',
      onNewFlags(flags) {
        console.log(flags);
        // → 'm'
      }
    })
    

Caveats

  • Lookbehind assertions cannot be transformed to older syntax.
  • When using namedGroups: 'transform', regexpu-core only takes care of the syntax: you will still need a runtime wrapper around the regular expression to populate the .groups property of RegExp.prototype.match()'s result. If you are using regexpu-core via Babel, it's handled automatically.

For maintainers

How to publish a new release

  1. 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.

  2. Push the release commit and tag:

    git push --follow-tags
    

    Our CI then automatically publishes the new release to npm.

  3. Once the release has been published to npm, update regexpu to make use of it, and cut a new release of regexpu as well.

Author

twitter/mathias
Mathias Bynens

License

regexpu-core is available under the MIT license.

Current Tags

  • 5.3.2                                ...           latest (2 years ago)

46 Versions

  • 5.3.2                                ...           2 years ago
  • 5.3.1                                ...           2 years ago
  • 5.3.0                                ...           2 years ago
  • 5.2.2                                ...           2 years ago
  • 5.2.1                                ...           2 years ago
  • 5.1.0                                ...           2 years ago
  • 5.0.1                                ...           3 years ago
  • 5.0.0                                ...           3 years ago
  • 4.8.0                                ...           3 years ago
  • 4.7.1                                ...           4 years ago
  • 4.7.0                                ...           5 years ago
  • 4.6.0                                ...           5 years ago
  • 4.5.5                                ...           5 years ago
  • 4.5.4                                ...           6 years ago
  • 4.5.3                                ...           6 years ago
  • 4.5.2                                ...           6 years ago
  • 4.5.1                                ...           6 years ago
  • 4.5.0                                ...           6 years ago
  • 4.4.0                                ...           6 years ago
  • 4.3.0                                ...           6 years ago
  • 4.2.0                                ...           7 years ago
  • 4.1.5                                ...           7 years ago
  • 4.1.4                                ...           7 years ago
  • 4.1.3                                ...           7 years ago
  • 4.1.2                                ...           7 years ago
  • 4.1.1                                ...           7 years ago
  • 4.1.0                                ...           7 years ago
  • 4.0.11                                ...           8 years ago
  • 4.0.10                                ...           8 years ago
  • 4.0.9                                ...           8 years ago
  • 4.0.8                                ...           8 years ago
  • 4.0.7                                ...           8 years ago
  • 4.0.6                                ...           8 years ago
  • 4.0.5                                ...           8 years ago
  • 4.0.4                                ...           8 years ago
  • 4.0.3                                ...           8 years ago
  • 4.0.2                                ...           8 years ago
  • 4.0.1                                ...           8 years ago
  • 4.0.0                                ...           8 years ago
  • 3.3.0                                ...           8 years ago
  • 3.2.0                                ...           8 years ago
  • 3.1.0                                ...           8 years ago
  • 3.0.2                                ...           8 years ago
  • 3.0.1                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 1.0.0                                ...           9 years ago

Copyright 2013 - present © cnpmjs.org | Home |