本文译自 6 JavaScript Regular Expression features you can use today
正则表达式虽然非常强大,但众所周知很难掌握。这里有 6 个有用的特性,可以帮助你开始在你的 JavaScript 项目中使用它们:
捕获组
捕获组允许您获取匹配字符串的特定部分,只需将部分正则表达式括在括号中即可 (...)
:
const str = 'JavaScript is a programming language';
/(JavaScript) is a (.*)/.exec(str)
/*
[
0: 'JavaScript is a programming language',
1: 'JavaScript',
2: 'programming language'
...
]
*/
非捕获组
非捕获组用于匹配某些内容而不捕获它,例如您并不真正需要的一个 /
或匹配组。它们的定义类似于捕获组,但前缀为 ?:
:
const str = 'JavaScript is a programming language';
/(?:JavaScript|Python) is a (.+)/.exec(str)
/*
[
0: 'JavaScript is a programming language',
1: 'programming language'
...
]
*/
命名捕获组
命名捕获组允许您命名捕获组,通过在它前面加上前缀 <name>
:
const str = 'JavaScript is a programming language';
/(?<subject>.+) is a (?<description>.+)/.exec(str)
/*
[
0: 'JavaScript is a programming language',
1: 'JavaScript',
2: 'programming language',
groups: {
subject: 'JavaScript,
description: 'programming language'
}
...
]
*/
捕获组反向引用
反向引用帮你写较短的正则表达式,通过重复现有的捕获组,使用 \1
,\2
等等。同样地,你也可以重复命名捕捉使用群体 \k
<name>
:
const str = 'JavaScript is a programming language - an awesome programming language JavaScript is';
/(.+) is a (?<description>.+) - an awesome \k<description> \1 is/.exec(str)
/*
[
0: 'JavaScript is a programming language - an awesome programming language JavaScript is',
1: 'JavaScript',
2: 'programming language',
groups: {
description: 'programming language'
}
...
]
*/
前瞻
前瞻(Lookaheads)允许您检查某些内容是否遵循特定模式,而无需实际匹配它。您可以使用以下方法创建正先行断言(?=
)和负先行断言(?!
):
const str = 'JavaScript is not the same as Java and you should remember that';
/Java(?=Script)(.*)/.exec(str)
/*
[
0: 'JavaScript is not the same as Java and you should remember that',
1: 'Script is not the same as Java and you should remember that'
...
]
*/
/Java(?!Script)(.*)/.exec(str)
/*
[
0: 'Java and you should remember that',
1: ' and you should remember that'
...
]
*/
Unicode 字符
最后,您可以匹配 unicode 字符,使用 /p{...}
和 /u
标志。示例包括但不限于 {Emoji}
,{Math_Symbols}
和 {Script=Greek}
:
const str = 'Greek looks like this: γεια';
/\p{Script=Greek}+/u.exec(str)
/*
[
0: 'γεια'
...
]
*/