You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
7.9 KiB

3 months ago
  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. _regexpu_ is a source code transpiler that enables the use of ES2015 Unicode regular expressions in JavaScript-of-today (ES5).
  3. _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.
  4. ## Installation
  5. To use _regexpu-core_ programmatically, install it as a dependency via [npm](https://www.npmjs.com/):
  6. ```bash
  7. npm install regexpu-core --save
  8. ```
  9. Then, `require` it:
  10. ```js
  11. const rewritePattern = require('regexpu-core');
  12. ```
  13. ## API
  14. This module exports a single function named `rewritePattern`.
  15. ### `rewritePattern(pattern, flags, options)`
  16. 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.
  17. ```js
  18. rewritePattern('foo.bar', 'u', { unicodeFlag: "transform" });
  19. // → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF])bar'
  20. rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'u', { unicodeFlag: "transform" });
  21. // → '(?:[a-z]|\\uD834[\\uDF06-\\uDF08])'
  22. rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'ui', { unicodeFlag: "transform" });
  23. // → '(?:[a-z\\u017F\\u212A]|\\uD834[\\uDF06-\\uDF08])'
  24. ```
  25. _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:
  26. ```js
  27. // In ES5, the dot operator only matches BMP symbols:
  28. rewritePattern('foo.bar', '', { unicodeFlag: "transform" });
  29. // → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF])bar'
  30. // But with the ES2015 `u` flag, it matches astral symbols too:
  31. rewritePattern('foo.bar', 'u', { unicodeFlag: "transform" });
  32. // → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF])bar'
  33. ```
  34. The optional `options` argument recognizes the following properties:
  35. #### Stable regular expression features
  36. 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.
  37. - `unicodeFlag` - The `u` flag, enabling support for Unicode code point escapes in the form `\u{...}`.
  38. ```js
  39. rewritePattern('\\u{ab}', '', {
  40. unicodeFlag: 'transform'
  41. });
  42. // → '\\u{ab}'
  43. rewritePattern('\\u{ab}', 'u', {
  44. unicodeFlag: 'transform'
  45. });
  46. // → '\\xAB'
  47. ```
  48. - `dotAllFlag` - The [`s` (`dotAll`) flag](https://github.com/mathiasbynens/es-regexp-dotall-flag).
  49. ```js
  50. rewritePattern('.', '', {
  51. dotAllFlag: 'transform'
  52. });
  53. // → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]'
  54. rewritePattern('.', 's', {
  55. dotAllFlag: 'transform'
  56. });
  57. // → '[\\0-\\uFFFF]'
  58. rewritePattern('.', 'su', {
  59. dotAllFlag: 'transform'
  60. });
  61. // → '(?:[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])'
  62. ```
  63. - `unicodePropertyEscapes` - [Unicode property escapes](property-escapes.md).
  64. 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.
  65. ```js
  66. rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
  67. unicodePropertyEscapes: 'transform'
  68. });
  69. // → '[\\u{14400}-\\u{14646}]'
  70. rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
  71. unicodeFlag: 'transform',
  72. unicodePropertyEscapes: 'transform'
  73. });
  74. // → '(?:\\uD811[\\uDC00-\\uDE46])'
  75. ```
  76. - `namedGroups` - [Named capture groups](https://github.com/tc39/proposal-regexp-named-groups).
  77. ```js
  78. rewritePattern('(?<name>.)\\k<name>', '', {
  79. namedGroups: 'transform'
  80. });
  81. // → '(.)\1'
  82. ```
  83. - `unicodeSetsFlag` - [The `v` (`unicodeSets`) flag](https://github.com/tc39/proposal-regexp-set-notation)
  84. ```js
  85. rewritePattern('[\\p{Emoji}&&\\p{ASCII}]', 'v', {
  86. unicodeSetsFlag: 'transform'
  87. });
  88. // → '[#\\*0-9]'
  89. ```
  90. 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.
  91. ```js
  92. rewritePattern('[^[a-h]&&[f-z]]', 'v', {
  93. unicodeSetsFlag: 'transform'
  94. });
  95. // → '[^f-h]' (to be used with /u)
  96. ```
  97. ```js
  98. rewritePattern('[^[a-h]&&[f-z]]', 'v', {
  99. unicodeSetsFlag: 'transform',
  100. unicodeFlag: 'transform'
  101. });
  102. // → '(?:(?![f-h])[\s\S])' (to be used without /u)
  103. ```
  104. - `modifiers` - [Inline `i`/`m`/`s` modifiers](https://github.com/tc39/proposal-regexp-modifiers)
  105. ```js
  106. rewritePattern('(?i:[a-z])[a-z]', '', {
  107. modifiers: 'transform'
  108. });
  109. // → '(?:[a-zA-Z])([a-z])'
  110. ```
  111. #### Experimental regular expression features
  112. 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.
  113. 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`.
  114. #### Miscellaneous options
  115. - `onNamedGroup`
  116. This option is a function that gets called when a named capture group is found. It receives two parameters:
  117. the name of the group, and its index.
  118. ```js
  119. rewritePattern('(?<name>.)\\k<name>', '', {
  120. onNamedGroup(name, index) {
  121. console.log(name, index);
  122. // → 'name', 1
  123. }
  124. });
  125. ```
  126. - `onNewFlags`
  127. This option is a function that gets called to pass the flags that the resulting pattern must be interpreted with.
  128. ```js
  129. rewritePattern('abc', 'um', '', {
  130. unicodeFlag: 'transform',
  131. onNewFlags(flags) {
  132. console.log(flags);
  133. // → 'm'
  134. }
  135. })
  136. ```
  137. ### Caveats
  138. - [Lookbehind assertions](https://github.com/tc39/proposal-regexp-lookbehind) cannot be transformed to older syntax.
  139. - 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.
  140. ## For maintainers
  141. ### How to publish a new release
  142. 1. On the `main` branch, bump the version number in `package.json`:
  143. ```sh
  144. npm version patch -m 'Release v%s'
  145. ```
  146. Instead of `patch`, use `minor` or `major` [as needed](https://semver.org/).
  147. Note that this produces a Git commit + tag.
  148. 1. Push the release commit and tag:
  149. ```sh
  150. git push --follow-tags
  151. ```
  152. Our CI then automatically publishes the new release to npm.
  153. 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).
  154. ## Author
  155. | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
  156. |---|
  157. | [Mathias Bynens](https://mathiasbynens.be/) |
  158. ## License
  159. _regexpu-core_ is available under the [MIT](https://mths.be/mit) license.