regexpu-core

中文文档
6.5.4发布于 2026/11/05MITTypeScriptCommonJS构建来源已验证近 30 天下载 2亿

regexpu’s core functionality (i.e. rewritePattern(pattern, flag)), capable of translating ES6 Unicode regular expressions to ES5.

原始使用文档
文档目录8 节
  1. Installation
  2. API
  3. rewritePattern(pattern, flags, options)
  4. Caveats
  5. For maintainers
  6. How to publish a new release
  7. Author
  8. License

regexpu-core Build status regexpu-core on npm

regexpu is a source code transpiler that enables the use of ES2015 Unicode regular expressions in JavaScript-of-today (ES5).

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 into equivalent ES5-compatible regular expression patterns.

Installation

To use regexpu-core programmatically, install it as a dependency via npm:

npm install regexpu-core --save

Then, require it:

const rewritePattern = require('regexpu-core');

API

This module exports a single function named rewritePattern.

rewritePattern(pattern, flags, options)

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.

rewritePattern('foo.bar', 'u', { unicodeFlag: "transform" });
// → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])bar'

rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'u', { unicodeFlag: "transform" });
// → '(?:[a-z]|\\uD834[\\uDF06-\\uDF08])'

rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'ui', { unicodeFlag: "transform" });
// → '(?:[a-z\\u017F\\u212A]|\\uD834[\\uDF06-\\uDF08])'

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:

// In ES5, the dot operator only matches BMP symbols:
rewritePattern('foo.bar', '', { unicodeFlag: "transform" });
// → 'foo.bar'

// But with the ES2015 `u` flag, it matches astral symbols too:
rewritePattern('foo.bar', 'u', { unicodeFlag: "transform" });
// → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])bar'

The optional options argument recognizes the following properties:

Stable regular expression features

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.

  • unicodeFlag - The u flag, enabling support for Unicode code point escapes in the form \u{...}.

    rewritePattern('\\u{ab}', '', {
      unicodeFlag: 'transform'
    });
    // → '\\u{ab}'
    
    rewritePattern('\\u{ab}', 'u', {
      unicodeFlag: 'transform'
    });
    // → '\\xAB'
    
  • dotAllFlag - The s (dotAll) flag.

    rewritePattern('.', '', {
      dotAllFlag: 'transform'
    });
    // → '.'
    
    rewritePattern('.', 's', {
      dotAllFlag: 'transform'
    });
    // → '[^]'
    
    rewritePattern('.', 'su', {
      dotAllFlag: 'transform'
    });
    // → '[^]'
    
  • unicodePropertyEscapes - Unicode property escapes.

    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.

    rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
      unicodePropertyEscapes: 'transform'
    });
    // → '[\\u{14400}-\\u{14646}]'
    
    rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
      unicodeFlag: 'transform',
      unicodePropertyEscapes: 'transform'
    });
    // → '(?:\\uD811[\\uDC00-\\uDE46])'
    
  • namedGroups - Named capture groups.

    rewritePattern('(?<name>.)\\k<name>', '', {
      namedGroups: 'transform'
    });
    // → '(.)\1'
    
  • unicodeSetsFlag - The v (unicodeSets) flag

    rewritePattern('[\\p{Emoji}&&\\p{ASCII}]', 'v', {
      unicodeSetsFlag: 'transform'
    });
    // → '[#\\*0-9]'
    

    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.

    rewritePattern('[^[a-h]&&[f-z]]', 'v', {
      unicodeSetsFlag: 'transform'
    });
    // → '[^f-h]' (to be used with /u)
    
    rewritePattern('[^[a-h]&&[f-z]]', 'v', {
      unicodeSetsFlag: 'transform',
      unicodeFlag: 'transform'
    });
    // → '(?:[\0-ei-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])' (to be used without /u)
    
  • modifiers - Inline i/m/s modifiers

    rewritePattern('(?i:[a-z])[a-z]', '', {
      modifiers: 'transform'
    });
    // → '(?:[A-Za-z])[a-z]'
    

Experimental regular expression features

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.

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.

Miscellaneous options

  • onNamedGroup

    This option is a function that gets called when a named capture group is found. It receives two parameters: the name of the group, and its index.

    rewritePattern('(?<name>.)\\k<name>', '', {
      onNamedGroup(name, index) {
        console.log(name, index);
        // → 'name', 1
      }
    });
    
  • onNewFlags

    This option is a function that gets called to pass the flags that the resulting pattern must be interpreted with.

    rewritePattern('abc', 'um', '', {
      unicodeFlag: 'transform',
      onNewFlags(flags) {
        console.log(flags);
        // → 'm'
      }
    })
    

Caveats

  • Lookbehind assertions cannot be transformed to older syntax.
  • 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.

For maintainers

How to publish a new release

  1. On the main branch, bump the version number in package.json:

    npm version patch -m 'Release v%s'
    

    Instead of patch, use minor or major as needed.

    Note that this produces a Git commit + tag.

  2. Push the release commit and tag:

    git push --follow-tags
    

    Our CI then automatically publishes the new release to npm.

  3. Once the release has been published to npm, update regexpu to make use of it, and cut a new release of regexpu as well.

Author

twitter/mathias
Mathias Bynens

License

regexpu-core is available under the MIT license.

版本与发布记录

regexpu-core 共发布 57 个版本,首次发布于 2016/01/12;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.5.4latest2026/11/051 天前
6.5.32026/10/061 个月前
6.5.22026/09/291 个月前
6.4.02025/09/221.1 年前
6.3.12025/09/101.2 年前
6.3.02025/09/101.2 年前

关于 regexpu-core 的常见问题

regexpu-core 是什么?

regexpu’s core functionality (i.e. rewritePattern(pattern, flag)), capable of translating ES6 Unicode regular expressions to ES5.

如何安装 regexpu-core?

在项目目录运行 npm install regexpu-core,或使用 pnpm add / yarn add / bun add。

regexpu-core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

regexpu-core 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

regexpu-core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=4。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

regexpu-core 可以商用吗?

regexpu-core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

regexpu-core 的下载量有多少?

根据 npm 官方下载统计,regexpu-core 在 2026/10/05 – 2026/11/03 这 30 天内被下载 201,438,572 次,日均约 6,714,619 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

regexpu-core 还在维护吗?

最新版本 6.5.4 发布于 2026/11/05(1 天前);至今共发布 57 个版本,其中过去 12 个月发布了 3 个;首次发布于 2016/01/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。