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.

146 lines
4.9 KiB

4 years ago
  1. # regexpu-core [![Build status](https://travis-ci.org/mathiasbynens/regexpu-core.svg?branch=master)](https://travis-ci.org/mathiasbynens/regexpu-core) [![Code coverage status](https://img.shields.io/codecov/c/github/mathiasbynens/regexpu-core.svg)](https://codecov.io/gh/mathiasbynens/regexpu-core)
  2. _regexpu_ is a source code transpiler that enables the use of ES6 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 ES6 `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');
  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');
  21. // → '(?:[a-z]|\\uD834[\\uDF06-\\uDF08])'
  22. rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'ui');
  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');
  29. // → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF])bar'
  30. // But with the ES6 `u` flag, it matches astral symbols too:
  31. rewritePattern('foo.bar', 'u');
  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. #### `dotAllFlag` (default: `false`)
  36. Setting this option to `true` enables support for [the `s` (`dotAll`) flag](https://github.com/mathiasbynens/es-regexp-dotall-flag).
  37. ```js
  38. rewritePattern('.');
  39. // → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]'
  40. rewritePattern('.', '', {
  41. 'dotAllFlag': true
  42. });
  43. // → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]'
  44. rewritePattern('.', 's', {
  45. 'dotAllFlag': true
  46. });
  47. // → '[\\0-\\uFFFF]'
  48. rewritePattern('.', 'su', {
  49. 'dotAllFlag': true
  50. });
  51. // → '(?:[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])'
  52. ```
  53. #### `unicodePropertyEscape` (default: `false`)
  54. Setting this option to `true` enables [support for Unicode property escapes](property-escapes.md):
  55. ```js
  56. rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
  57. 'unicodePropertyEscape': true
  58. });
  59. // → '(?:\\uD811[\\uDC00-\\uDE46])'
  60. ```
  61. #### `lookbehind` (default: `false`)
  62. Setting this option to `true` enables support for [lookbehind assertions](https://github.com/tc39/proposal-regexp-lookbehind).
  63. ```js
  64. rewritePattern('(?<=.)a', '', {
  65. 'lookbehind': true
  66. });
  67. // → '(?<=[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF])a'
  68. ```
  69. #### `namedGroup` (default: `false`)
  70. Setting this option to `true` enables support for [named capture groups](https://github.com/tc39/proposal-regexp-named-groups).
  71. ```js
  72. rewritePattern('(?<name>.)\k<name>', '', {
  73. 'namedGroups': true
  74. });
  75. // → '(.)\1'
  76. ```
  77. #### `onNamedGroup`
  78. This option is a function that gets called when a named capture group is found. It receives two parameters:
  79. the name of the group, and its index.
  80. ```js
  81. rewritePattern('(?<name>.)\k<name>', '', {
  82. 'namedGroups': true,
  83. onNamedGroup(name, index) {
  84. console.log(name, index);
  85. // → 'name', 1
  86. }
  87. });
  88. ```
  89. #### `useUnicodeFlag` (default: `false`)
  90. 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 ES6 `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.
  91. ```js
  92. rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
  93. 'unicodePropertyEscape': true,
  94. 'useUnicodeFlag': true
  95. });
  96. // → '[\\u{14400}-\\u{14646}]'
  97. ```
  98. ## Author
  99. | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
  100. |---|
  101. | [Mathias Bynens](https://mathiasbynens.be/) |
  102. ## License
  103. _regexpu-core_ is available under the [MIT](https://mths.be/mit) license.