飞雪连天射白鹿,笑书神侠倚碧鸳

0%

用api的方式写出正则表达式

super-expressive库及其api文档
【翻译】对应的中文文档方便理解

一年前做编辑器二次开发相关项目时,涉及了正则处理就好好学习了下,然而现在都忘完了,最近发现了这个开源工具,通过api的方式生成所需要的正则表达式,COOL!

截至目前super-expressive star 3k,但是还没有中文文档,截至发文时有50个

本篇是一篇翻译后的API中文文档,对示例将保持原样


API

api列表 - [SuperExpressive()](#SuperExpressive) - [.allowMultipleMatches](#allowMultipleMatches) - [.lineByLine](#lineByLine) - [.caseInsensitive](#caseInsensitive) - [.sticky](#sticky) - [.unicode](#unicode) - [.singleLine](#singleLine) - [.anyChar](#anyChar) - [.whitespaceChar](#whitespaceChar) - [.nonWhitespaceChar](#nonWhitespaceChar) - [.digit](#digit) - [.nonDigit](#nonDigit) - [.word](#word) - [.nonWord](#nonWord) - [.wordBoundary](#wordBoundary) - [.nonWordBoundary](#nonWordBoundary) - [.newline](#newline) - [.carriageReturn](#carriageReturn) - [.tab](#tab) - [.nullByte](#nullByte) - [.anyOf](#anyOf) - [.capture](#capture) - [.namedCapture(name)](#namedCapturename) - [.backreference(index)](#backreferenceindex) - [.namedBackreference(index)](#namedBackreferenceindex) - [.group](#group) - [.end()](#end()) - [.assertAhead](#assertAhead) - [.assertNotAhead](#assertNotAhead) - [.optional](#optional) - [.zeroOrMore](#zeroOrMore) - [.zeroOrMoreLazy](#zeroOrMoreLazy) - [.oneOrMore](#oneOrMore) - [.oneOrMoreLazy](#oneOrMoreLazy) - [.exactly(n)](#exactlyn) - [.atLeast(n)](#atLeastn) - [.between(x, y)](#betweenx-y) - [.betweenLazy(x, y)](#betweenLazyx-y) - [.startOfInput](#startOfInput) - [.endOfInput](#endOfInput) - [.anyOfChars(chars)](#anyOfCharschars) - [.anythingButChars(chars)](#anythingButCharschars) - [.anythingButString(str)](#anythingButStringstr) - [.anythingButRange(a, b)](#anythingButRangea-b) - [.string(s)](#strings) - [.char(c)](#charc) - [.range(a, b)](#rangea-b) - [.subexpression(expr, opts)](#subexpressionexpr-opts) - [.toRegexString()](#toRegexString) - [.toRegex()](#toRegex)

SuperExpressive()

创建一个实例

.allowMultipleMatches

使用修饰符/g,表示查找所有可能的匹配

Example:

1
2
3
4
5
6
SuperExpressive()
.allowMultipleMatches
.string('hello')
.toRegex();
// ->
/hello/g

.lineByLine

使用修饰符/m,表示多行匹配

字符串无论是否换行只有一个开始^和结尾$,
如果采用多行匹配,那么每一个行都有一个^和结尾$

.startOfInput.endOfInput标记作为行的开始和结束

Example:

1
2
3
4
5
6
SuperExpressive()
.lineByLine
.string('^hello$')
.toRegex();
// ->
/\^hello\$/m

.caseInsensitive

使用修饰符/i,表示匹配时应忽略大小写区分

Example:

1
2
3
4
5
6
SuperExpressive()
.caseInsensitive
.string('HELLO')
.toRegex();
// ->
/HELLO/i

.sticky

使用修饰符/y,表示匹配时从lastIndex位置开始进行匹配,直到匹配失败时停止

Example:

1
2
3
4
5
6
SuperExpressive()
.sticky
.string('hello')
.toRegex();
// ->
/hello/y

.unicode

使用修饰符/u,表示使用完全unicode匹配,即四个字节的UTF-16编码/Unicode字符

Example:

1
2
3
4
5
6
SuperExpressive()
.unicode
.string('héllo')
.toRegex();
// ->
/héllo/u

.singleLine

使用修饰符/s,表示单行匹配

.startOfInput.endOfInput标记作为行的开始和结束,和.anyChar一起用时表示匹配新行

Example:

1
2
3
4
5
6
7
8
SuperExpressive()
.singleLine
.string('hello')
.anyChar
.string('world')
.toRegex();
// ->
/hello.world/s

.anyChar

元字符.,匹配除换行以外的任何单个字符,和.singleLine一起用时表示匹配新行

Example:

1
2
3
4
5
SuperExpressive()
.anyChar
.toRegex();
// ->
/./

.whitespaceChar

元字符\s,匹配任何【空白】字符,【包括】特殊的空白字符:\r\n\t\f\v

Example:

1
2
3
4
5
SuperExpressive()
.whitespaceChar
.toRegex();
// ->
/\s/

.nonWhitespaceChar

元字符/S,匹配任何【非空白】字符,【不包括】特殊空格字符:\r\n\t\f\v

Example:

1
2
3
4
5
SuperExpressive()
.nonWhitespaceChar
.toRegex();
// ->
/\S/

.digit

元字符/d,匹配“0-9”中的任何数字

Example:

1
2
3
4
5
SuperExpressive()
.digit
.toRegex();
// ->
/\d/

.nonDigit

元字符/D,匹配任何非数字

Example:

1
2
3
4
5
SuperExpressive()
.nonDigit
.toRegex();
// ->
/\D/

.word

元字符/w,匹配任何字母数字(a-z,a-z,0-9)字符以及_

Example:

1
2
3
4
5
SuperExpressive()
.word
.toRegex();
// ->
/\w/

.nonWord

元字符/W,匹配任何非字母数字(a-z,a-z,0-9)字符,也不包括_

Example:

1
2
3
4
5
SuperExpressive()
.nonWord
.toRegex();
// ->
/\W/

.wordBoundary

立即匹配(不消耗任何字符)

.word匹配的字符和.word不匹配的字符之间(以任何顺序)

Example:

1
2
3
4
5
6
SuperExpressive()
.digit
.wordBoundary
.toRegex();
// ->
/\d\b/

.nonWordBoundary

在由[.word](#word)匹配的两个字符之间的位置匹配(不消耗任何字符)。

Example:

1
2
3
4
5
6
SuperExpressive()
.digit
.nonWordBoundary
.toRegex();
// ->
/\d\B/

.newline

元字符\n,匹配换行符

Example:

1
2
3
4
5
SuperExpressive()
.newline
.toRegex();
// ->
/\n/

.carriageReturn

元字符\r,匹配回车符

Example:

1
2
3
4
5
SuperExpressive()
.carriageReturn
.toRegex();
// ->
/\r/

.tab

元字符\t,匹配制表符

Example:

1
2
3
4
5
SuperExpressive()
.tab
.toRegex();
// ->
/\t/

.nullByte

元字符\0,匹配空字符,null或\u0000(Unicode十六进制编码)

Example:

1
2
3
4
5
SuperExpressive()
.nullByte
.toRegex();
// ->
/\0/

.anyOf

匹配指定元素之间的选择。需要用“.end()”完成

Example:

1
2
3
4
5
6
7
8
9
SuperExpressive()
.anyOf
.range('a', 'f')
.range('0', '9')
.string('XXX')
.end()
.toRegex();
// ->
/(?:XXX|[a-f0-9])/

.capture

为正在处理的元素创建一个捕获组。需要用“.end()”完成,之后用backreference(index)引用

Example:

1
2
3
4
5
6
7
8
9
SuperExpressive()
.capture
.range('a', 'f')
.range('0', '9')
.string('XXX')
.end()
.toRegex();
// ->
/([a-f][0-9]XXX)/

.namedCapture(name)

为后续元素创建命名的捕获组。需要用“.end()”完成。以后可以用namedBackreference(name)backreference(index)引用。

Example:

1
2
3
4
5
6
7
8
9
SuperExpressive()
.namedCapture('interestingStuff')
.range('a', 'f')
.range('0', '9')
.string('XXX')
.end()
.toRegex();
// ->
/(?<interestingStuff>[a-f][0-9]XXX)/

.namedBackreference(name)

完全匹配之前由namedCapture匹配的内容。

Example:

1
2
3
4
5
6
7
8
9
10
11
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)

使用位置索引精确匹配之前由capturenamedCapture匹配的内容。注意regex索引从1开始,因此第一个捕获组的索引为1。

Example:

1
2
3
4
5
6
7
8
9
10
11
SuperExpressive()
.capture
.range('a', 'f')
.range('0', '9')
.string('XXX')
.end()
.string('something else')
.backreference(1)
.toRegex();
// ->
/([a-f][0-9]XXX)something else\1/

.group

创建一个非捕获的处理元素组。需要用“.end()”完成。

Example:

1
2
3
4
5
6
7
8
9
SuperExpressive()
.optional.group
.range('a', 'f')
.range('0', '9')
.string('XXX')
.end()
.toRegex();
// ->
/(?:[a-f][0-9]XXX)?/

.end()

表示SuperExpressive分组的结束,例如.anyOf.group.capture

Example:

1
2
3
4
5
6
7
8
9
10
11
SuperExpressive()
.capture
.anyOf
.range('a', 'f')
.range('0', '9')
.string('XXX')
.end()
.end()
.toRegex();
// ->
/((?:XXX|[a-f0-9]))/

.assertAhead

断言,在不使用进程元素的情况下找到它们。需要用“.end()”完成。

Example:

1
2
3
4
5
6
7
8
SuperExpressive()
.assertAhead
.range('a', 'f')
.end()
.range('a', 'z')
.toRegex();
// ->
/(?=[a-f])[a-z]/

.assertNotAhead

断言,在没有使用这些元素的情况下找不到。需要用“.end()”完成。

Example:

1
2
3
4
5
6
7
8
SuperExpressive()
.assertNotAhead
.range('a', 'f')
.end()
.range('g', 'z')
.toRegex();
// ->
/(?![a-f])[g-z]/

.optional

断言,进程元素可能匹配,也可能不匹配

Example:

1
2
3
4
5
SuperExpressive()
.optional.digit
.toRegex();
// ->
/\d?/

.zeroOrMore

断言,进程元素可能不匹配,或者可能多次匹配。

Example:

1
2
3
4
5
SuperExpressive()
.zeroOrMore.digit
.toRegex();
// ->
/\d*/

.zeroOrMoreLazy

断言,进程元素可能不匹配,或者可能匹配多次,但要尽可能少

Example:

1
2
3
4
5
SuperExpressive()
.zeroOrMoreLazy.digit
.toRegex();
// ->
/\d*?/

.oneOrMore

断言,进程元素可以匹配一次,也可以匹配多次

Example:

1
2
3
4
5
SuperExpressive()
.oneOrMore.digit
.toRegex();
// ->
/\d+/

.oneOrMoreLazy

断言,继续元素可以匹配一次,也可以匹配多次,但要尽可能少

Example:

1
2
3
4
5
SuperExpressive()
.oneOrMoreLazy.digit
.toRegex();
// ->
/\d+?/

.exactly(n)

断言,前一个元素将精确匹配“n”次

Example:

1
2
3
4
5
SuperExpressive()
.exactly(5).digit
.toRegex();
// ->
/\d{5}/

.atLeast(n)

断言,继续的元素将至少匹配“n”次

Example:

1
2
3
4
5
SuperExpressive()
.atLeast(5).digit
.toRegex();
// ->
/\d{5,}/

.between(x, y)

断言,前一个元素将在“x”和“y”时间之间匹配

Example:

1
2
3
4
5
SuperExpressive()
.between(3, 5).digit
.toRegex();
// ->
/\d{3,5}/

.betweenLazy(x, y)

断言,前一个元素将在“x”和“y”时间之间匹配,但要尽可能少地匹配。

Example:

1
2
3
4
5
SuperExpressive()
.betweenLazy(3, 5).digit
.toRegex();
// ->
/\d{3,5}?/

.startOfInput

使用.lineByLine时,断言输入的开始或行的开始

Example:

1
2
3
4
5
6
SuperExpressive()
.startOfInput
.string('hello')
.toRegex();
// ->
/^hello/

.endOfInput

使用.lineByLine时,断言输入的结尾或行的结尾

Example:

1
2
3
4
5
6
SuperExpressive()
.string('hello')
.endOfInput
.toRegex();
// ->
/hello$/

.anyOfChars(chars)

匹配提供的字符串“chars”中的任何字符

Example:

1
2
3
4
5
SuperExpressive()
.anyOfChars('aeiou')
.toRegex();
// ->
/[aeiou]/

.anythingButChars(chars)

匹配任何字符,但提供的字符串“chars”中的任何字符除外

Example:

1
2
3
4
5
SuperExpressive()
.anythingButChars('aeiou')
.toRegex();
// ->
/[^aeiou]/

.anythingButString(str)

匹配与“str”长度相同的任何字符串,但“str”中按顺序定义的字符除外

Example:

1
2
3
4
5
SuperExpressive()
.anythingButString('aeiou')
.toRegex();
// ->
/(?:[^a][^e][^i][^o][^u])/

.anythingButRange(a, b)

匹配任何字符,但由“a”和“b”指定的.range捕获的字符除外

Example:

1
2
3
4
5
SuperExpressive()
.anythingButRange(0, 9)
.toRegex();
// ->
/[^0-9]/

.string(s)

与字符串“s”完全匹配

Example:

1
2
3
4
5
SuperExpressive()
.string('hello')
.toRegex();
// ->
/hello/

.char(c)

与字符“c”完全匹配

Example:

1
2
3
4
5
SuperExpressive()
.char('x')
.toRegex();
// ->
/x/

.range(a, b)

匹配“a”和“b”之间的任何字符。排序由字符ASCII或unicode值定义

Example:

1
2
3
4
5
SuperExpressive()
.range('a', 'z')
.toRegex();
// ->
/[a-z]/

.subexpression(expr, opts?)

  • opts.命名空间,指在子表达式中的所有命名捕获组上使用字符串格式的命名空间,以避免与您自己命名的组发生命名冲突(默认='')
  • opts.忽略标志,如果设置为true,则应忽略此子表达式指定的任何标志(默认 = true)
  • opts.忽略开始和结束,如果设置为true,则应忽略此子表达式指定的任何startOfInput/endOfInput(默认 = true)

匹配内联的另一个SuperExpressive实例。可用于创建库,或将代码模块化. 默认情况下,将忽略标志和输入标记的开始/结束, 但是可以在options对象中显式地打开

Example:

1
2
3
4
5
6
7
8
9
10
// A reusable SuperExpressive...
const fiveDigits = SuperExpressive().exactly(5).digit;

SuperExpressive()
.oneOrMore.range('a', 'z')
.atLeast(3).anyChar
.subexpression(fiveDigits)
.toRegex();
// ->
/[a-z]+.{3,}\d{5}/

.toRegexString()

输出SuperExpression模型的正则表达式的字符串形式

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SuperExpressive()
.allowMultipleMatches
.lineByLine
.startOfInput
.optional.string('0x')
.capture
.exactly(4).anyOf
.range('A', 'F')
.range('a', 'f')
.range('0', '9')
.end()
.end()
.endOfInput
.toRegexString();
// ->
"/^(?:0x)?([A-Fa-f0-9]{4})$/gm"

.toRegex()

输出SuperExpression模型的正则表达式

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SuperExpressive()
.allowMultipleMatches
.lineByLine
.startOfInput
.optional.string('0x')
.capture
.exactly(4).anyOf
.range('A', 'F')
.range('a', 'f')
.range('0', '9')
.end()
.end()
.endOfInput
.toRegex();
// ->
/^(?:0x)?([A-Fa-f0-9]{4})$/gm
听说,打赏我的人最后都找到了真爱
↘ 此处应有打赏 ↙
// 用户脚本