百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

super-expressive

> 编程语言
开源

Super Expressive 是一款零依赖的 JavaScript 库,用于以 (几乎) 自然语言构建正则表达式

4.6K stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Super Expressive 是一款零依赖的 JavaScript 库,用于以 (几乎) 自然语言构建正则表达式

Super Expressive

Super Expressive is a JavaScript library that allows you to build regular expressions in almost natural language - with no extra dependencies, and a lightweight code footprint (less than 4kb with minification + gzip!).


  • Why

  • Installation and Usage

  • Example

  • Playground

  • Ports

  • API

    Click to expand
    • SuperExpressive()
    • .allowMultipleMatches
    • .lineByLine
    • .caseInsensitive
    • .generateIndices
    • .sticky
    • .unicode
    • .singleLine
    • .anyChar
    • .whitespaceChar
    • .nonWhitespaceChar
    • .digit
    • .nonDigit
    • .word
    • .nonWord
    • .wordBoundary
    • .nonWordBoundary
    • .newline
    • .carriageReturn
    • .tab
    • .verticalTab
    • .formFeed
    • .backspace
    • .nullByte
    • .anyOf
    • .capture
    • .namedCapture(name)
    • .backreference(index)
    • .namedBackreference(index)
    • .group
    • .end()
    • .assertAhead
    • .assertNotAhead
    • .assertBehind
    • .assertNotBehind
    • .optional
    • .zeroOrMore
    • .zeroOrMoreLazy
    • .oneOrMore
    • .oneOrMoreLazy
    • .exactly(n)
    • .atLeast(n)
    • .atLeastLazy(n)
    • .between(x, y)
    • .betweenLazy(x, y)
    • .startOfInput
    • .endOfInput
    • .anyOfChars(chars)
    • .anythingBut
    • .anythingButChars(chars)
    • .anythingButString(str)
    • .anythingButRange(a, b)
    • .string(s)
    • .char(c)
    • .controlChar(c)
    • .hexCode(hex)
    • .utf16Code(hex)
    • .unicodeCharCode(hex)
    • .unicodeProperty(property)
    • .notUnicodeProperty(property)
    • .range(a, b)
    • .subexpression(expr, opts)
    • .toRegexString()
    • .toRegex()

Why?

Regex is a very powerful tool, but its terse and cryptic vocabulary can make constructing and communicating them with others a challenge. Even developers who understand them well can have trouble reading their own back just a few months later! In addition, they can't be easily created and manipulated in a programmatic way - closing off an entire avenue of dynamic text processing.

That's where Super Expressive comes in. It provides a programmatic and human readable way to create regular expressions. It's API uses the fluent builder pattern, and is completely immutable. It's built to be discoverable and predictable:

  • properties and methods describe what they do in plain English
  • order matters! quantifiers are specified before the thing they change, just like in English (e.g. SuperExpressive().exactly(5).digit)
  • if you make a mistake, you'll know how to fix it. SuperExpressive will guide you towards a fix if your expression is invalid
  • subexpressions can be used to create meaningful, reusable components
  • includes an index.d.ts file for full TypeScript support

SuperExpressive turns those complex and unwieldy regexes that appear in code reviews into something that can be read, understood, and properly reviewed by your peers - and maintained by anyone!

Installation and Usage

npm i super-expressive
const SuperExpressive = require('super-expressive');

// Or as an ES6 module
import SuperExpressive from 'super-expressive';

Example

The following example recognises and captures the value of a 16-bit hexadecimal number like 0xC0D3.

const SuperExpressive = require('super-expressive');

const myRegex = SuperExpressive()
  .startOfInput
  .optional.string('0x')
  .capture
    .exactly(4).anyOf
      .range('A', 'F')
      .range('a', 'f')
      .range('0', '9')
    .end()
  .end()
  .endOfInput
  .toRegex();

// Produces the following regular expression:
/^(?:0x)?([A-Fa-f0-9]{4})$/

Playground

You can experiment with SuperExpressive in the Super Expressive Playground by @nartc. This is a great way to build a regex description, and test it against various inputs.

Ports

Super Expressive has been ported to the following languages:

PHP

https://github.com/bassim/super-expressive-php by @bassim

Ruby

https://github.com/hiy/super-expressive-ruby by @hiy

Python

https://github.com/stanislav-tsaplev/super_expressive by @stanislav-tsaplev

API

SuperExpressive()

SuperExpressive()

Creates an instance of SuperExpressive.

.allowMultipleMatches

Uses the g flag on the regular expression, which indicates that it should match multiple values when run on a string.

Example

SuperExpressive()
  .allowMultipleMatches
  .string('hello')
  .toRegex();
// ->
/hello/g

.lineByLine

Uses the m flag on the regular expression, which indicates that it should treat the .startOfInput and .endOfInput markers as the start and end of lines.

Example

SuperExpressive()
  .lineByLine
  .string('^hello$')
  .toRegex();
// ->
/\^hello\$/m

.caseInsensitive

Uses the i flag on the regular expression, which indicates that it should treat ignore the uppercase/lowercase distinction when matching.

Example

SuperExpressive()
  .caseInsensitive
  .string('HELLO')
  .toRegex();
// ->
/HELLO/i

.generateIndices

Uses the d flag on the regular expression, which indicates that it should generate indices for the start and end of each capture group.

Example

SuperExpressive()
  .generateIndices
  .string('hello')
  .toRegex();
// ->
/hello/d

.sticky

Uses the y flag on the regular expression, which indicates that it should create a stateful regular expression that can be resumed from the last match.

Example

SuperExpressive()
  .sticky
  .string('hello')
  .toRegex();
// ->
/hello/y

.unicode

Uses the u flag on the regular expression, which indicates that it should use full unicode matching.

Example

SuperExpressive()
  .unicode
  .string('héllo')
  .toRegex();
// ->
/héllo/u

.singleLine

Uses the s flag on the regular expression, which indicates that the input should be treated as a single line, where the .startOfInput and .endOfInput markers explicitly mark the start and end of input, and .anyChar also matches newlines.

Example

SuperExpressive()
  .singleLine
  .string('hello')
  .anyChar
  .string('world')
  .toRegex();
// ->
/hello.world/s

.anyChar

Matches any single character. When combined with .singleLine, it also matches newlines.

Example

SuperExpressive()
  .anyChar
  .toRegex();
// ->
/./

.whitespaceChar

Matches any whitespace character, including the special whitespace characters: \r\n\t\f\v.

Example

SuperExpressive()
  .whitespaceChar
  .toRegex();
// ->
/\s/

.nonWhitespaceChar

Matches any non-whitespace character, excluding also the special whitespace characters: \r\n\t\f\v.

Example

SuperExpressive()
  .nonWhitespaceChar
  .toRegex();
// ->
/\S/

.digit

Matches any digit from 0-9.

Example

SuperExpressive()
  .digit
  .toRegex();
// ->
/\d/

.nonDigit

Matches any non-digit.

Example

SuperExpressive()
  .nonDigit
  .toRegex();
// ->
/\D/

.word

Matches any alpha-numeric (a-z, A-Z, 0-9) characters, as well as _.

Example

SuperExpressive()
  .word
  .toRegex();
// ->
/\w/

.nonWord

Matches any non alpha-numeric (a-z, A-Z, 0-9) characters, excluding _ as well.

Example

SuperExpressive()
  .nonWord
  .toRegex();
// ->
/\W/

.wordBoundary

Matches (without consuming any characters) immediately between a character matched by .word and a character not matched by .word (in either order).

Example

SuperExpressive()
  .digit
  .wordBoundary
  .toRegex();
// ->
/\d\b/

.nonWordBoundary

Matches (without consuming any characters) at the position between two characters matched by .word.

Example

SuperExpressive()
  .digit
  .nonWordBoundary
  .toRegex();
// ->
/\d\B/

.newline

Matches a \n character.

Example

SuperExpressive()
  .newline
  .toRegex();
// ->
/\n/

.carriageReturn

Matches a \r character.

Example

SuperExpressive()
  .carriageReturn
  .toRegex();
// ->
/\r/

.tab

Matches a \t character.

Example

SuperExpressive()
  .tab
  .toRegex();
// ->
/\t/

.verticalTab

Matches a \v character.

Example

SuperExpressive()
  .verticalTab
  .toRegex();
// ->
/\v/

.formFeed

Matches a \f character.

Example

SuperExpressive()
  .formFeed
  .toRegex();
// ->
/\f/

.backspace

Matches a \b character.

Example

SuperExpressive()
  .backspace
  .toRegex();
// ->
/[\b]/

.nullByte

Matches a \u0000 character (ASCII 0).

Example

SuperExpressive()
  .nullByte
  .toRegex();
// ->
/\0/

.anyOf

Matches a choice between specified elements. Needs to be finalised with .end().

Example

SuperExpressive()
  .anyOf
    .range('a', 'f')
    .range('0', '9')
    .string('XXX')
  .end()
  .toRegex();
// ->
/(?:XXX|[a-f0-9])/

.capture

Creates a capture group for the proceeding elements. Needs to be finalised with .end(). Can be later referenced with backreference(index).

Example

SuperExpressive()
  .capture
    .range('a', 'f')
    .range('0', '9')
    .string('XXX')
  .end()
  .toRegex();
// ->
/([a-f][0-9]XXX)/

.namedCapture(name)

Creates a named capture group for the proceeding elements. Needs to be finalised with .end(). Can be later referenced with namedBackreference(name) or backreference(index).

Example

SuperExpressive()
  .namedCapture('interestingStuff')
    .range('a', 'f')
    .range('0', '9')
    .string('XXX')
  .end()
  .toRegex();
// ->
/(?<interestingStuff>[a-f][0-9]XXX)/

.namedBackreference(name)

Matches exactly what was previously matched by a namedCapture.

Example

SuperExpressive()
  .namedCapture('interestingStuff')
    .range('a', 'f')
    .range('0', '9')
    .string('XXX')
  .end()
  .string('something else')
  .namedBackreference('interestingStuff')
  .toRegex();
// ->
/(?<interestingStuff>[a-f][0-9]XXX)something else\k<interestingStuff>/

.backreference(index)

Matches exactly what was previously matched by a capture or namedCapture using a posi

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •Installation and Usage
  • •Playground
  • •SuperExpressive()
  • •.allowMultipleMatches
  • •.lineByLine
  • •.caseInsensitive
  • •.generateIndices
  • •.unicode
  • •.singleLine
  • •.anyChar

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言