@ark/regex

中文文档
0.0.14发布于 2026/10/21MITTypeScriptES Module构建来源已验证近 30 天下载 12.8万

A drop-in replacement for new RegExp() with types

此软件包已被标记为弃用

@ark/regex is no longer published. Install arkregex instead.

原始使用文档
文档目录3 节
  1. Usage
  2. Features
  3. FAQ

arkregex

A drop-in replacement for new RegExp() with types

Usage

The regex function creates a Regex instance with types for .test(), .exec() and more, statically parsed from native JS syntax:

import { regex } from "arkregex"

const ok = regex("^ok$", "i")
// Regex<"ok" | "oK" | "Ok" | "OK", { flags: "i" }>

const semver = regex("^(\\d*)\\.(\\d*)\\.(\\d*)$")
// Regex<`${number}.${number}.${number}`, { captures: [`${number}`, `${number}`, `${number}`] }>

const email = regex("^(?<name>\\w+)@(?<domain>\\w+\\.\\w+)$")
// Regex<`${string}@${string}.${string}`, { names: { name: string; domain: `${string}.${string}`; }; ...>

All you need to get started is pnpm install arkregex (or the equivalent for your package manager of choice) 🎉

Performs best with TS 5.9+

Features

  • Types: Infers string types for your existing regular expressions, including positional and named captures
  • Parity: Supports 100% of features allowed by new RegExp()
  • Safety: Syntax errors like referencing a group that doesn't exist are now type errors
  • Zero Runtime: Improves your type safety without impacting your bundle size

FAQ

Why aren't some patterns like [a-Z] inferred more precisely?

Constructing string literal types for these sorts of expressions is combinatorial and will explode very quickly if we infer character ranges like this as literal characters.

We've tried to strike a balance between performance and precision while guaranteeing that the inferred types are at worst imprecise and never incorrect.

Why doesn't it work with my massive RegExp?

If your expression is especially long or complex, TypeScript won't be able to infer it.

If your types start to slow down or you see the dreaded Type is excessively deep..., you can manually type your expression using regex.as:

const complexPattern = regex.as<`pattern-${string}`, { captures: [string] }>(
    "very-long-complex-expression-here"
)

Is it robust?

arkregex types are extensively tested and benchmarked using attest.

If anything not covered by the other FAQs is not behaving how you'd expect, please don't hesitate to create an issue.

How can I get syntax highlighting for regex?

The ArkType extension can be installed to add syntax highlighting to regex calls.

版本与发布记录

@ark/regex 共发布 14 个版本,首次发布于 2025/10/14;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.14latest2026/10/2118 天前
0.0.132026/10/0830 天前
0.0.122026/10/021 个月前
0.0.112026/10/011 个月前
0.0.102026/09/251 个月前
0.0.82026/07/084 个月前

关于 @ark/regex 的常见问题

@ark/regex 是什么?

A drop-in replacement for new RegExp() with types注意:该包已被作者标记为弃用——@ark/regex is no longer published. Install arkregex instead.

如何安装 @ark/regex?

在项目目录运行 npm install @ark/regex,或使用 pnpm add / yarn add / bun add。

@ark/regex 支持 TypeScript 吗?

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

@ark/regex 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ark/regex 可以商用吗?

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

@ark/regex 的下载量有多少?

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

@ark/regex 还在维护吗?

最新版本 0.0.14 发布于 2026/10/21(18 天前);至今共发布 14 个版本,其中过去 12 个月发布了 11 个;首次发布于 2025/10/14。作者已将其标记为弃用,新项目建议寻找替代方案。