Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/2fd/rregex
Rust Regex binding for Javascript
https://github.com/2fd/rregex
regex rust typescript
Last synced: 3 days ago
JSON representation
Rust Regex binding for Javascript
- Host: GitHub
- URL: https://github.com/2fd/rregex
- Owner: 2fd
- License: apache-2.0
- Created: 2021-12-02T01:58:15.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-12-30T19:23:10.000Z (12 days ago)
- Last Synced: 2025-01-02T06:12:19.901Z (10 days ago)
- Topics: regex, rust, typescript
- Language: JavaScript
- Homepage: https://rregex.dev
- Size: 4.27 MB
- Stars: 30
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-regex - rregex - Bindings for Rust's `regex`, a non-backtracking engine. (JavaScript regex libraries / Alternative regex builders and engines)
README
# rregex
A dependency-free WebAssembly build of [Rust Regex](https://docs.rs/regex/latest/regex/) for Javascript
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/2fd/rregex/release.yml?branch=master) ![GitHub Release](https://img.shields.io/github/v/release/2fd/rregex) ![NPM Version](https://img.shields.io/npm/v/rregex)
[![JSR](https://jsr.io/badges/@rregex/rregex)](https://jsr.io/@rregex/rregex)- [Why Rust Regex](#why-rust-regex)
- [Install](#install)
- [Supported Runtimes](#supported-runtimes)
- [API](https://tsdocs.dev/docs/rregex/latest)
- [`RRegex`](https://tsdocs.dev/docs/rregex/latest/classes/RRegex.html)
- [`RRegexSet`](https://tsdocs.dev/docs/rregex/latest/classes/RRegexSet.html)
- [`Match`](https://tsdocs.dev/docs/rregex/latest/types/Match.html)
- [`Captures`](https://tsdocs.dev/docs/rregex/latest/types/Captures.html)
- [`escape`](https://tsdocs.dev/docs/rregex/latest/functions/escape.html)
- [Benchmarks](#benchmarks)
- [Known Issues](#known-issues)## Why Rust Regex
Rust has a powerful Regex library with a lot of features that don't exists en the standard `Regex` object
[See the official documentation](https://docs.rs/regex/latest/regex/#syntax) for more detail
## Install
```bash
# NPM
npm install rregex# Yarn
yarn add rregex# PNPM
pnpm add rregex# Deno
deno add @rregex/rregex# JSR
npx jsr add @rregex/rregex
```## Supported Runtimes
This package includes builds for multiple runtimes
| Runtime | Import | version |
| ------------------ | ------------------------------------------------------- | ---------- |
| Node.js (esm) | `import { RRegex, RRegexSet } from 'rregex'` | `*` |
| Node.js (commonjs) | `const { RRegex, RRegexSet } = require('rregex')` | `*` |
| Deno | `import { RRegex, RRegexSet } from '@rregex/rregex'` | `>=1.10.8` |
| Bun | `import { RRegex, RRegexSet } from '@rregex/rregex'` | `>=1.10.8` |
| Cloudflare Workers | `import { RRegex, RRegexSet } from 'rregex/lib/cf.mjs'` | `>=1.10.8` |
| Browser | TODO | |
| Standalone | TODO | |## Benchmarks
In general terms `rregex` is at least 1 order of magnitud slower than the native `RegExp` object, but still have a good performance. **Unless you required some of the features that `rregex` provides, you should always consider using the native `RegExp` object**
> Benchmarks are executed using [test/deno.bench.mjs](./test/deno.bench.mjs), and [test/node.bench.mjs](./test/node.bench.mjs)
| benchmark | time (avg) | iter/s |
| --------- | -------------: | ------: |
| RegExp | 342.91 µs/iter | 2,916.2 |
| RRegex | 7.27 ms/iter | 137.6 |> summary: `RegExp` is 21.19x faster than `RRegex`
### IP
| benchmark | time (avg) | iter/s |
| --------- | ------------: | -------: |
| RegExp | 72.63 µs/iter | 13,767.7 |
| RRegex | 7.02 ms/iter | 142.5 |> summary: `RegExp` is 96.59x faster than `RRegex`
### URI
| benchmark | time (avg) | iter/s |
| --------- | -------------: | ----------: |
| RegExp | 688.79 ns/iter | 1,451,825.0 |
| RRegex | 22.52 ms/iter | 44.4 |> summary: `RegExp` is 32689.42x faster than `RRegex`
| benckmarks | benckmarks |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [![Simple IP](./img/rregex-vs-native_-simple-ip.png)](https://jsbenchmark.com/#eyJjYXNlcyI6W3siaWQiOiJlZl9VbGJvYThpRGNYeGM3VUl4YnMiLCJjb2RlIjoiREFUQS5tYXRjaCgnKD86KD86MjVbMC01XXwyWzAtNF1bMC05XXxbMDFdP1swLTldWzAtOV0pXFxcXC4pezN9KD86MjVbMC01XXwyWzAtNF1bMC05XXxbMDFdP1swLTldWzAtOV0pJykiLCJkZXBlbmRlbmNpZXMiOltdLCJuYW1lIjoiUmVnRXhwOiBJUCJ9LHsiaWQiOiI0QkxuZmtPd0p4ZTZhQ0NSVmpIOVUiLCJjb2RlIjoibmV3IHJyZWdleC5SUmVnZXgoXCIoPzooPzoyNVswLTVdfDJbMC00XVswLTldfFswMV0_WzAtOV1bMC05XSlcXFxcLil7M30oPzoyNVswLTVdfDJbMC00XVswLTldfFswMV0_WzAtOV1bMC05XSlcIikuZmluZChEQVRBKSIsImRlcGVuZGVuY2llcyI6W10sIm5hbWUiOiJSUmVnZXg6IElQIn1dLCJjb25maWciOnsibmFtZSI6IlJSZWdleCB2cyBOYXRpdmU6IElQIiwicGFyYWxsZWwiOnRydWUsImdsb2JhbFRlc3RDb25maWciOnsiZGVwZW5kZW5jaWVzIjpbeyJ1cmwiOiJodH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VsaXZyLm5ldC9ucG0vcnJlZ2V4QDEuMTAuMTAvbGliL3JyZWdleC53YXNtJylcbmNvbnN0IHcgPSBhd2FpdCB3cmVzLmFycmF5QnVmZmVyKClcbnJyZWdleC5pbml0U3luYyh3KVxuXG5yZXR1cm4gZGF0YS5zbGljZSgwLCBNYXRoLmZsb29yKGRhdGEubGVuZ3RoLzUpKSJ9fQ) |> **Note:** In order to compare with native regex these benchmarks follow the [`mariomka/regex-benchmark`](https://github.com/mariomka/regex-benchmark) structure
## Known Issues
If you call `splitn(text, limit)` and the expected result length is equal to `limit - 1` the result will include an extra item `""`, this behavior does not happen if `limit` es greater. **`fixed at >=1.3`**
```ts
const regex = new RRegex(",");
expect(regex.splitn("a,b,c", 0)).toEqual([]);
expect(regex.splitn("a,b,c", 1)).toEqual(["a,b,c"]);
expect(regex.splitn("a,b,c", 2)).toEqual(["a", "b,c"]);
expect(regex.splitn("a,b,c", 3)).toEqual(["a", "b", "c"]);// This result includes an unexpected extra item
expect(regex.splitn("a,b,c", 4)).toEqual(["a", "b", "c", ""]);
expect(regex.splitn("a,b,c", 5)).toEqual(["a", "b", "c"]);expect(regex.splitn("abc", 0)).toEqual([]);
expect(regex.splitn("abc", 1)).toEqual(["abc"]);// This result includes an unexpected extra item
expect(regex.splitn("abc", 2)).toEqual(["abc", ""]);
expect(regex.splitn("abc", 3)).toEqual(["abc"]);
```