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', { unicodeFlag: "transform" });
|
---|
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', { unicodeFlag: "transform" });
|
---|
34 | // → '(?:[a-z]|\\uD834[\\uDF06-\\uDF08])'
|
---|
35 |
|
---|
36 | rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'ui', { unicodeFlag: "transform" });
|
---|
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', '', { unicodeFlag: "transform" });
|
---|
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', { unicodeFlag: "transform" });
|
---|
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 | #### Stable regular expression features
|
---|
55 |
|
---|
56 | 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.
|
---|
57 |
|
---|
58 | - `unicodeFlag` - The `u` flag, enabling support for Unicode code point escapes in the form `\u{...}`.
|
---|
59 |
|
---|
60 | ```js
|
---|
61 | rewritePattern('\\u{ab}', '', {
|
---|
62 | unicodeFlag: 'transform'
|
---|
63 | });
|
---|
64 | // → '\\u{ab}'
|
---|
65 |
|
---|
66 | rewritePattern('\\u{ab}', 'u', {
|
---|
67 | unicodeFlag: 'transform'
|
---|
68 | });
|
---|
69 | // → '\\xAB'
|
---|
70 | ```
|
---|
71 |
|
---|
72 | - `dotAllFlag` - The [`s` (`dotAll`) flag](https://github.com/mathiasbynens/es-regexp-dotall-flag).
|
---|
73 |
|
---|
74 | ```js
|
---|
75 | rewritePattern('.', '', {
|
---|
76 | dotAllFlag: 'transform'
|
---|
77 | });
|
---|
78 | // → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]'
|
---|
79 |
|
---|
80 | rewritePattern('.', 's', {
|
---|
81 | dotAllFlag: 'transform'
|
---|
82 | });
|
---|
83 | // → '[\\0-\\uFFFF]'
|
---|
84 |
|
---|
85 | rewritePattern('.', 'su', {
|
---|
86 | dotAllFlag: 'transform'
|
---|
87 | });
|
---|
88 | // → '(?:[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])'
|
---|
89 | ```
|
---|
90 |
|
---|
91 | - `unicodePropertyEscapes` - [Unicode property escapes](property-escapes.md).
|
---|
92 |
|
---|
93 | 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.
|
---|
94 |
|
---|
95 | ```js
|
---|
96 | rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
|
---|
97 | unicodePropertyEscapes: 'transform'
|
---|
98 | });
|
---|
99 | // → '[\\u{14400}-\\u{14646}]'
|
---|
100 |
|
---|
101 | rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
|
---|
102 | unicodeFlag: 'transform',
|
---|
103 | unicodePropertyEscapes: 'transform'
|
---|
104 | });
|
---|
105 | // → '(?:\\uD811[\\uDC00-\\uDE46])'
|
---|
106 | ```
|
---|
107 |
|
---|
108 | - `namedGroups` - [Named capture groups](https://github.com/tc39/proposal-regexp-named-groups).
|
---|
109 |
|
---|
110 | ```js
|
---|
111 | rewritePattern('(?<name>.)\\k<name>', '', {
|
---|
112 | namedGroups: 'transform'
|
---|
113 | });
|
---|
114 | // → '(.)\1'
|
---|
115 | ```
|
---|
116 |
|
---|
117 | - `unicodeSetsFlag` - [The `v` (`unicodeSets`) flag](https://github.com/tc39/proposal-regexp-set-notation)
|
---|
118 |
|
---|
119 | ```js
|
---|
120 | rewritePattern('[\\p{Emoji}&&\\p{ASCII}]', 'v', {
|
---|
121 | unicodeSetsFlag: 'transform'
|
---|
122 | });
|
---|
123 | // → '[#\\*0-9]'
|
---|
124 | ```
|
---|
125 |
|
---|
126 | 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.
|
---|
127 |
|
---|
128 | ```js
|
---|
129 | rewritePattern('[^[a-h]&&[f-z]]', 'v', {
|
---|
130 | unicodeSetsFlag: 'transform'
|
---|
131 | });
|
---|
132 | // → '[^f-h]' (to be used with /u)
|
---|
133 | ```
|
---|
134 |
|
---|
135 | ```js
|
---|
136 | rewritePattern('[^[a-h]&&[f-z]]', 'v', {
|
---|
137 | unicodeSetsFlag: 'transform',
|
---|
138 | unicodeFlag: 'transform'
|
---|
139 | });
|
---|
140 | // → '(?:(?![f-h])[\s\S])' (to be used without /u)
|
---|
141 | ```
|
---|
142 |
|
---|
143 | - `modifiers` - [Inline `i`/`m`/`s` modifiers](https://github.com/tc39/proposal-regexp-modifiers)
|
---|
144 |
|
---|
145 | ```js
|
---|
146 | rewritePattern('(?i:[a-z])[a-z]', '', {
|
---|
147 | modifiers: 'transform'
|
---|
148 | });
|
---|
149 | // → '(?:[a-zA-Z])([a-z])'
|
---|
150 | ```
|
---|
151 |
|
---|
152 | #### Experimental regular expression features
|
---|
153 |
|
---|
154 | 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.
|
---|
155 |
|
---|
156 | 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`.
|
---|
157 |
|
---|
158 | #### Miscellaneous options
|
---|
159 |
|
---|
160 | - `onNamedGroup`
|
---|
161 |
|
---|
162 | This option is a function that gets called when a named capture group is found. It receives two parameters:
|
---|
163 | the name of the group, and its index.
|
---|
164 |
|
---|
165 | ```js
|
---|
166 | rewritePattern('(?<name>.)\\k<name>', '', {
|
---|
167 | onNamedGroup(name, index) {
|
---|
168 | console.log(name, index);
|
---|
169 | // → 'name', 1
|
---|
170 | }
|
---|
171 | });
|
---|
172 | ```
|
---|
173 |
|
---|
174 | - `onNewFlags`
|
---|
175 |
|
---|
176 | This option is a function that gets called to pass the flags that the resulting pattern must be interpreted with.
|
---|
177 |
|
---|
178 | ```js
|
---|
179 | rewritePattern('abc', 'um', '', {
|
---|
180 | unicodeFlag: 'transform',
|
---|
181 | onNewFlags(flags) {
|
---|
182 | console.log(flags);
|
---|
183 | // → 'm'
|
---|
184 | }
|
---|
185 | })
|
---|
186 | ```
|
---|
187 |
|
---|
188 | ### Caveats
|
---|
189 |
|
---|
190 | - [Lookbehind assertions](https://github.com/tc39/proposal-regexp-lookbehind) cannot be transformed to older syntax.
|
---|
191 | - 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.
|
---|
192 |
|
---|
193 | ## For maintainers
|
---|
194 |
|
---|
195 | ### How to publish a new release
|
---|
196 |
|
---|
197 | 1. On the `main` branch, bump the version number in `package.json`:
|
---|
198 |
|
---|
199 | ```sh
|
---|
200 | npm version patch -m 'Release v%s'
|
---|
201 | ```
|
---|
202 |
|
---|
203 | Instead of `patch`, use `minor` or `major` [as needed](https://semver.org/).
|
---|
204 |
|
---|
205 | Note that this produces a Git commit + tag.
|
---|
206 |
|
---|
207 | 1. Push the release commit and tag:
|
---|
208 |
|
---|
209 | ```sh
|
---|
210 | git push --follow-tags
|
---|
211 | ```
|
---|
212 |
|
---|
213 | Our CI then automatically publishes the new release to npm.
|
---|
214 |
|
---|
215 | 1. Once the release has been published to npm, update [`regexpu`](https://github.com/mathiasbynens/regexpu) to make use of it, and [cut a new release of `regexpu` as well](https://github.com/mathiasbynens/regexpu#how-to-publish-a-new-release).
|
---|
216 |
|
---|
217 |
|
---|
218 | ## Author
|
---|
219 |
|
---|
220 | | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
|
---|
221 | |---|
|
---|
222 | | [Mathias Bynens](https://mathiasbynens.be/) |
|
---|
223 |
|
---|
224 | ## License
|
---|
225 |
|
---|
226 | _regexpu-core_ is available under the [MIT](https://mths.be/mit) license.
|
---|