a406eb88b69dde6512ca35247c27375bd9aafd6b0a1f0f6e6893347b3b97ae8daa0f39bd3d06c0092e0a7cdec4b6cc7dd6c52f50d0a583a1b49b30f45972b9 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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](http://img.shields.io/coveralls/mathiasbynens/regexpu-core/master.svg)](https://coveralls.io/r/mathiasbynens/regexpu-core) [![Dependency status](https://gemnasium.com/mathiasbynens/regexpu-core.svg)](https://gemnasium.com/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-dev
  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)`
  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. `rewritePattern` uses [regjsgen](https://github.com/d10/regjsgen), [regjsparser](https://github.com/jviereck/regjsparser), and [regenerate](https://github.com/mathiasbynens/regenerate) as internal dependencies.
  35. ## Author
  36. | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
  37. |---|
  38. | [Mathias Bynens](https://mathiasbynens.be/) |
  39. ## License
  40. _regexpu-core_ is available under the [MIT](https://mths.be/mit) license.