[6a3a178] | 1 | # regexpu-core [![Build status](https://github.com/mathiasbynens/regexpu-core/workflows/run-checks/badge.svg)](https://github.com/mathiasbynens/regexpu-core/actions?query=workflow%3Arun-checks) [![regexpu-core on npm](https://img.shields.io/npm/v/regexpu-core)](https://www.npmjs.com/package/regexpu-core)
|
---|
| 2 |
|
---|
| 3 | _regexpu_ is a source code transpiler that enables the use of ES2015 Unicode regular expressions in JavaScript-of-today (ES5).
|
---|
| 4 |
|
---|
| 5 | _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](https://mathiasbynens.be/notes/es6-unicode-regex) into equivalent ES5-compatible regular expression patterns.
|
---|
| 6 |
|
---|
| 7 | ## Installation
|
---|
| 8 |
|
---|
| 9 | To use _regexpu-core_ programmatically, install it as a dependency via [npm](https://www.npmjs.com/):
|
---|
| 10 |
|
---|
| 11 | ```bash
|
---|
| 12 | npm install regexpu-core --save
|
---|
| 13 | ```
|
---|
| 14 |
|
---|
| 15 | Then, `require` it:
|
---|
| 16 |
|
---|
| 17 | ```js
|
---|
| 18 | const rewritePattern = require('regexpu-core');
|
---|
| 19 | ```
|
---|
| 20 |
|
---|
| 21 | ## API
|
---|
| 22 |
|
---|
| 23 | This module exports a single function named `rewritePattern`.
|
---|
| 24 |
|
---|
| 25 | ### `rewritePattern(pattern, flags, options)`
|
---|
| 26 |
|
---|
| 27 | 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.
|
---|
| 28 |
|
---|
| 29 | ```js
|
---|
| 30 | rewritePattern('foo.bar', 'u');
|
---|
| 31 | // → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF])bar'
|
---|
| 32 |
|
---|
| 33 | rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'u');
|
---|
| 34 | // → '(?:[a-z]|\\uD834[\\uDF06-\\uDF08])'
|
---|
| 35 |
|
---|
| 36 | rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'ui');
|
---|
| 37 | // → '(?:[a-z\\u017F\\u212A]|\\uD834[\\uDF06-\\uDF08])'
|
---|
| 38 | ```
|
---|
| 39 |
|
---|
| 40 | _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:
|
---|
| 41 |
|
---|
| 42 | ```js
|
---|
| 43 | // In ES5, the dot operator only matches BMP symbols:
|
---|
| 44 | rewritePattern('foo.bar');
|
---|
| 45 | // → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF])bar'
|
---|
| 46 |
|
---|
| 47 | // But with the ES2015 `u` flag, it matches astral symbols too:
|
---|
| 48 | rewritePattern('foo.bar', 'u');
|
---|
| 49 | // → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF])bar'
|
---|
| 50 | ```
|
---|
| 51 |
|
---|
| 52 | The optional `options` argument recognizes the following properties:
|
---|
| 53 |
|
---|
| 54 | #### `dotAllFlag` (default: `false`)
|
---|
| 55 |
|
---|
| 56 | Setting this option to `true` enables support for [the `s` (`dotAll`) flag](https://github.com/mathiasbynens/es-regexp-dotall-flag).
|
---|
| 57 |
|
---|
| 58 | ```js
|
---|
| 59 | rewritePattern('.');
|
---|
| 60 | // → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]'
|
---|
| 61 |
|
---|
| 62 | rewritePattern('.', '', {
|
---|
| 63 | 'dotAllFlag': true
|
---|
| 64 | });
|
---|
| 65 | // → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]'
|
---|
| 66 |
|
---|
| 67 | rewritePattern('.', 's', {
|
---|
| 68 | 'dotAllFlag': true
|
---|
| 69 | });
|
---|
| 70 | // → '[\\0-\\uFFFF]'
|
---|
| 71 |
|
---|
| 72 | rewritePattern('.', 'su', {
|
---|
| 73 | 'dotAllFlag': true
|
---|
| 74 | });
|
---|
| 75 | // → '(?:[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])'
|
---|
| 76 | ```
|
---|
| 77 |
|
---|
| 78 | #### `unicodePropertyEscape` (default: `false`)
|
---|
| 79 |
|
---|
| 80 | Setting this option to `true` enables [support for Unicode property escapes](property-escapes.md):
|
---|
| 81 |
|
---|
| 82 | ```js
|
---|
| 83 | rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
|
---|
| 84 | 'unicodePropertyEscape': true
|
---|
| 85 | });
|
---|
| 86 | // → '(?:\\uD811[\\uDC00-\\uDE46])'
|
---|
| 87 | ```
|
---|
| 88 |
|
---|
| 89 | #### `lookbehind` (default: `false`)
|
---|
| 90 |
|
---|
| 91 | Setting this option to `true` enables support for [lookbehind assertions](https://github.com/tc39/proposal-regexp-lookbehind).
|
---|
| 92 |
|
---|
| 93 | ```js
|
---|
| 94 | rewritePattern('(?<=.)a', '', {
|
---|
| 95 | 'lookbehind': true
|
---|
| 96 | });
|
---|
| 97 | // → '(?<=[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF])a'
|
---|
| 98 | ```
|
---|
| 99 |
|
---|
| 100 | #### `namedGroup` (default: `false`)
|
---|
| 101 |
|
---|
| 102 | Setting this option to `true` enables support for [named capture groups](https://github.com/tc39/proposal-regexp-named-groups).
|
---|
| 103 |
|
---|
| 104 | ```js
|
---|
| 105 | rewritePattern('(?<name>.)\k<name>', '', {
|
---|
| 106 | 'namedGroup': true
|
---|
| 107 | });
|
---|
| 108 | // → '(.)\1'
|
---|
| 109 | ```
|
---|
| 110 |
|
---|
| 111 | #### `onNamedGroup`
|
---|
| 112 |
|
---|
| 113 | This option is a function that gets called when a named capture group is found. It receives two parameters:
|
---|
| 114 | the name of the group, and its index.
|
---|
| 115 |
|
---|
| 116 | ```js
|
---|
| 117 | rewritePattern('(?<name>.)\k<name>', '', {
|
---|
| 118 | 'namedGroup': true,
|
---|
| 119 | onNamedGroup(name, index) {
|
---|
| 120 | console.log(name, index);
|
---|
| 121 | // → 'name', 1
|
---|
| 122 | }
|
---|
| 123 | });
|
---|
| 124 | ```
|
---|
| 125 |
|
---|
| 126 | #### `useUnicodeFlag` (default: `false`)
|
---|
| 127 |
|
---|
| 128 | Setting this option to `true` enables the use of Unicode code point escapes of the form `\u{…}`. Note that in regular expressions, such escape sequences only work correctly when the ES2015 `u` flag is set. Enabling this setting often results in more compact output, although there are cases (such as `\p{Lu}`) where it actually _increases_ the output size.
|
---|
| 129 |
|
---|
| 130 | ```js
|
---|
| 131 | rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
|
---|
| 132 | 'unicodePropertyEscape': true,
|
---|
| 133 | 'useUnicodeFlag': true
|
---|
| 134 | });
|
---|
| 135 | // → '[\\u{14400}-\\u{14646}]'
|
---|
| 136 | ```
|
---|
| 137 |
|
---|
| 138 | ## For maintainers
|
---|
| 139 |
|
---|
| 140 | ### How to publish a new release
|
---|
| 141 |
|
---|
| 142 | 1. On the `main` branch, bump the version number in `package.json`:
|
---|
| 143 |
|
---|
| 144 | ```sh
|
---|
| 145 | npm version patch -m 'Release v%s'
|
---|
| 146 | ```
|
---|
| 147 |
|
---|
| 148 | Instead of `patch`, use `minor` or `major` [as needed](https://semver.org/).
|
---|
| 149 |
|
---|
| 150 | Note that this produces a Git commit + tag.
|
---|
| 151 |
|
---|
| 152 | 1. Push the release commit and tag:
|
---|
| 153 |
|
---|
| 154 | ```sh
|
---|
| 155 | git push && git push --tags
|
---|
| 156 | ```
|
---|
| 157 |
|
---|
| 158 | Our CI then automatically publishes the new release to npm.
|
---|
| 159 |
|
---|
| 160 | ## Author
|
---|
| 161 |
|
---|
| 162 | | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
|
---|
| 163 | |---|
|
---|
| 164 | | [Mathias Bynens](https://mathiasbynens.be/) |
|
---|
| 165 |
|
---|
| 166 | ## License
|
---|
| 167 |
|
---|
| 168 | _regexpu-core_ is available under the [MIT](https://mths.be/mit) license.
|
---|