@dashevo/wasm-re2

中文文档
2.0.4发布于 2023/07/31ISCTypeScriptCommonJS零依赖近 30 天下载 6336

RE2 for different platforms (Linux, Windows, macOS)

原始使用文档
文档目录21 节
  1. Getting Started
  2. Installation
  3. Usage
  4. API
  5. RE2.test(string)
  6. RE2.exec(string)
  7. RE2.replace(string, rewrite)
  8. RE2.numberOfCaptureGroups()
  9. Working with Emscripten
  10. For Mac
  11. For Windows
  12. Pull submodule google-re2
  13. Clone emsdk in your storage of repos
  14. Enter that directory
  15. Fetch the latest version of the emsdk (not needed the first…
  16. Download and install the latest SDK tools
  17. Make the "latest" SDK "active" for the current user. (write…
  18. Activate PATH and other environment variables in the curren…
  19. Compile C code
  20. Updating re2 to latest
  21. Rollup

RE2

Getting Started

Installation

npm i wasm-re2

Usage

import { RE2 } from 'wasm-re2';

const regex = new RE2('(b|^a)', 'g');

const isFound = regex.test('aabc');
// true

const matches = regex.exec('aabc');
// [ ['a', 'a'], ['b', 'b'] ]

API

RE2.test(string)

Executes a search for a match between a regular expression and a specified string.

Returns true or false.

RE2.exec(string)

Returns all matches of the regular expression against a string.

Works like non-iterable RegExp matchAll.

Exec returns all results in a 2D array where each result consists of [fullmatch, ...captureMathces].

Example:

const regex = new RE2('(abc)\\d+(zxc)', 'g');

regex.exec('123abc123zxc123abc123zxc123')
// [ ['abc123zxc', 'abc', 'zxc'], ['abc123zxc', 'abc', 'zxc'] ]

RE2.replace(string, rewrite)

Return a new string with some or all matches of a pattern replaced by a replacement.

RE2.numberOfCaptureGroups()

Returns number of capture groups.

Working with Emscripten

For Mac

If you use mac proceed to Pull submodule google-re2. You need node and python to be installed.

For Windows

Install Debian from Microsoft Store, then launch and create UNIX user.

Enter new UNIX username:
New password:
Retype new password:

Then launch:

sudo apt-get update -y && sudo apt-get install nodejs npm git python -y

Install Subsystem WSL using Setting on Windows 10:

  1. Open Settings.
  2. Click on Apps.
  3. Under the Related settings section, click the Programs and Features option.
  4. Click the Turn Windows features on or off option on the left pane.
  5. Check the Windows Subsystem for the Linux checkbox.
  6. Click the OK button.
  7. Then you need to reboot the PC.

Launch IDE with WSL, and do all next actions in WSL Terminal.

Pull submodule google-re2

git submodule update --init

Clone emsdk in your storage of repos

git clone https://github.com/emscripten-core/emsdk.git

Enter that directory

cd emsdk

Fetch the latest version of the emsdk (not needed the first time you clone)

git pull

Download and install the latest SDK tools

./emsdk install latest

Make the "latest" SDK "active" for the current user. (writes ~/.emscripten file)

./emsdk activate latest

Activate PATH and other environment variables in the current terminal

from emsdk dir: source ./emsdk_env.sh

from re2: source ../emsdk/emsdk_env.sh

Compile C code

Install dependencies

npm i

Update/create foundation folder running

npm run foundation

Make sure you have permissions to execute the file chmod +x ./compile.sh

Run script npm run compile

Testing

npm test

Updating re2 to latest

If google-re2 module is updated

git pull --recurse-submodules

Then run this script to update re2 package

npm run build:update

Rollup

To change rollup configuration read Rollup_FAQ.md in yaml-core package or visit https://rollupjs.org/guide/en/.

版本与发布记录

@dashevo/wasm-re2 共发布 5 个版本,首次发布于 2022/03/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.4latest2023/07/313 年前
2.0.32023/07/273 年前
1.0.22022/03/164 年前
1.0.12022/03/164 年前
1.0.02022/03/164 年前

关于 @dashevo/wasm-re2 的常见问题

@dashevo/wasm-re2 是什么?

RE2 for different platforms (Linux, Windows, macOS)

如何安装 @dashevo/wasm-re2?

在项目目录运行 npm install @dashevo/wasm-re2,或使用 pnpm add / yarn add / bun add。

@dashevo/wasm-re2 支持 TypeScript 吗?

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

@dashevo/wasm-re2 支持 ESM 还是 CommonJS?

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

@dashevo/wasm-re2 需要什么版本的 Node.js?

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

@dashevo/wasm-re2 可以商用吗?

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

@dashevo/wasm-re2 的下载量有多少?

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

@dashevo/wasm-re2 还在维护吗?

最新版本 2.0.4 发布于 2023/07/31(3 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/03/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。