Skip to content

Regular Expressions Pattern Syntax API

Regular expression pattern syntax for text matching, searching and replacing.

1 class · 10 methods

Token Reference

10 methods

正则表达式的元字符、量词、锚点、分组等核心语法。

.

匹配除换行符外的任意单个字符(默认不匹配 \n)。

Returns

匹配任意单字符

Example

regex
// JS 示例
/a.c/.test("abc");      // true
/a.c/.test("a c");      // true
/a.c/.test("ac");       // false(需有中间字符)

// 默认 . 不匹配换行
/a.b/.test("a\nb");    // false

// 用 s 标志(dotAll)可匹配换行
/a.b/s.test("a\nb");   // true
^ $

^ 匹配字符串开头,$ 匹配字符串结尾。多行模式下匹配行首/行尾。

Returns

锚定位置(零宽)

Example

regex
// 开头匹配
/^Hello/.test("Hello world");  // true
/^Hello/.test("Say Hello");    // false

// 结尾匹配
/world$/.test("Hello world");  // true

// 多行模式(每行独立)
/^[A-Z]/m.test("Line1\nbad"); // true
[abc] [^abc] [a-z]

字符类:匹配方括号内任意一个字符;^ 开头表示取反;- 表示范围。

Returns

匹配字符类中的一个字符

Example

regex
/[aeiou]/.test("hello");     // true(含元音)
/[^0-9]/.test("abc");       // true(非数字)
/[a-z]/.test("A");          // false(区分大小写)
/[a-zA-Z0-9_]/.test("_");   // true(单词字符)
\d \w \s \D \W \S

速记字符类:\d 数字、\w 单词字符、\s 空白;大写为取反。

Returns

匹配对应字符类

Example

regex
/\d+/.test("abc123");      // true
/\w+/.test("hello_1");    // true
/\s+/.test("a b");        // true
/\D+/.test("abc");        // true(非数字)
/\W/.test("!@#");         // true(非单词字符)
/\S+/.test("word");       // true(非空白)
+ * ?

量词:+ 一次或多次,* 零次或多次,? 零次或一次。

Returns

匹配重复次数

Example

regex
/ab+c/.test("ac");      // false(+ 至少一次)
/ab*c/.test("ac");      // true(* 可零次)
/ab?c/.test("ac");      // true(? 可省略)
/ab?c/.test("abbc");    // false(? 最多一次)

// 贪婪 vs 非贪婪(加 ?)
/a.+?b/.exec("axxbxxb")[0];  // "axxb"(最短)
{n} {n,} {n,m}

限定量词:恰好 n 次、至少 n 次、n 到 m 次。

Returns

匹配指定次数

Example

regex
/\d{4}/.test("2024");        // true(恰好 4 位)
/\d{2,}/.test("12345");    // true(至少 2 位)
/\d{2,4}/.test("123");     // true(2-4 位)
/\d{2,4}/.test("1");       // false(少于 2 位)
( ... ) (?: ... )

分组:() 捕获分组,(?:) 非捕获分组,可整体应用量词。

Returns

捕获分组可通过反向引用或 match 数组访问

Example

regex
// 捕获分组
const m = /(\d{4})-(\d{2})-(\d{2})/.exec("2024-01-15");
m[1];  // "2024"
m[2];  // "01"
m[3];  // "15"

// 非捕获分组(不占用编号)
/(?:foo)+bar/.test("foofoofoobar");  // true
|

选择(alternation):匹配左右任意一个表达式。

Returns

匹配分支之一

Example

regex
/cat|dog/.test("I have a cat");  // true
/cat|dog/.test("I have a dog");  // true

// 配合分组限定范围
/(java|type)script/.test("javascript");  // true
/(java|type)script/.test("typescript");  // true
\1 \2 ...

反向引用:引用前面捕获分组匹配到的相同文本。

Returns

匹配与对应分组相同的内容

Example

regex
// 匹配重复单词
/(\b\w+)\s+\1/.test("hello hello");  // true
/(\b\w+)\s+\1/.test("hello world");  // false

// 匹配成对引号
/(["'])(.*?)\1/.exec("'abc'")[0];  // "'abc'"
/(["'])(.*?)\1/.exec('"abc"')[0];  // '"abc"'
(?=...) (?!...) (?<=...) (?<!...)since ES2018

环视(前后查找):(?=) 正向前瞻,(?!) 负向前瞻;(?<=) 正向后瞻,(?<!) 负向后瞻。零宽断言。

Returns

匹配位置(零宽,不消耗字符)

Example

regex
// 正向前瞻:数字后跟 USD
/\d+(?=USD)/.exec("100USD");  // "100"

// 负向前瞻:数字后不跟 USD
/\d+(?!USD)/.exec("100EUR");  // "100"

// 正向后瞻:货币符号后的数字
/(?<=\$)\d+/.exec("$100");   // "100"

// 负向后瞻:非 $ 后的数字
/(?<!\$)\d+/.exec("€100");   // "100"