正则表达式

正则表达式是一种用于匹配字符串中字符组合的模式,在JavaScript中广泛应用于字符串搜索、替换和验证。

创建正则表达式

字面量方式

javascript
const regex = /pattern/flags;

构造函数方式

javascript
const regex = new RegExp('pattern', 'flags');

正则表达式标志

标志 描述
i 忽略大小写
g 全局匹配(找到所有匹配项)
m 多行匹配
s 允许.匹配换行符
u Unicode模式
y 粘性匹配

基本语法

字符类

模式 描述
[abc] 匹配a、b或c
[^abc] 匹配除a、b、c之外的任意字符
[a-z] 匹配a到z的任意小写字母
[A-Z] 匹配A到Z的任意大写字母
[0-9] 匹配0到9的任意数字
[a-zA-Z0-9] 匹配任意字母或数字

元字符

元字符 描述
. 匹配除换行符外的任意单个字符
\w 匹配字母、数字、下划线
\W 匹配非字母、数字、下划线
\d 匹配数字
\D 匹配非数字
\s 匹配空白字符(空格、制表符、换行符等)
\S 匹配非空白字符
\b 匹配单词边界
\B 匹配非单词边界
^ 匹配字符串开头
` 匹配字符串结尾

量词

量词 描述
* 匹配前面的表达式0次或多次
+ 匹配前面的表达式1次或多次
? 匹配前面的表达式0次或1次
{n} 匹配前面的表达式恰好n次
{n,} 匹配前面的表达式至少n次
{n,m} 匹配前面的表达式至少n次,最多m次

分组和捕获

javascript
// 分组
const regex = /(\d{3})-(\d{3}-\d{4})/;
const phone = '123-456-7890';
const match = phone.match(regex);
console.log(match[0]); // '123-456-7890'(完整匹配)
console.log(match[1]); // '123'(第一个分组)
console.log(match[2]); // '456-7890'(第二个分组)

// 命名捕获组
const regex2 = /(?<areaCode>\d{3})-(?<local>\d{3}-\d{4})/;
const match2 = phone.match(regex2);
console.log(match2.groups.areaCode); // '123'
console.log(match2.groups.local); // '456-7890'

或运算符

javascript
const regex = /apple|banana|orange/;
console.log(regex.test('I like apple')); // true
console.log(regex.test('I like banana')); // true
console.log(regex.test('I like grape')); // false

转义字符

使用\来转义特殊字符:

javascript
const regex = /\.\*\+/;
console.log(regex.test('.*+')); // true

正则表达式方法

RegExp对象方法

test()

检查字符串是否匹配正则表达式,返回布尔值:

javascript
const regex = /\d+/;
console.log(regex.test('123')); // true
console.log(regex.test('abc')); // false

exec()

执行匹配,返回匹配结果数组或null:

javascript
const regex = /(\w+)@(\w+)\.(\w+)/;
const email = 'user@example.com';

const match = regex.exec(email);
console.log(match[0]); // 'user@example.com'
console.log(match[1]); // 'user'
console.log(match[2]); // 'example'
console.log(match[3]); // 'com'

String对象方法

match()

获取匹配结果:

javascript
const text = 'I have 10 apples and 20 oranges';
const regex = /\d+/g;
const matches = text.match(regex);
console.log(matches); // ['10', '20']

search()

查找匹配的位置,返回索引或-1:

javascript
const text = 'I have 10 apples';
const regex = /\d+/;
const index = text.search(regex);
console.log(index); // 7

replace()

替换匹配的文本:

javascript
const text = 'I have 10 apples';
const regex = /\d+/;
const newText = text.replace(regex, '20');
console.log(newText); // 'I have 20 apples'

// 使用捕获组替换
const text2 = 'Hello, John Doe';
const regex2 = /(\w+), (\w+) (\w+)/;
const newText2 = text2.replace(regex2, '$2 $3, $1');
console.log(newText2); // 'John Doe, Hello'

split()

根据正则表达式分割字符串:

javascript
const text = 'apple, banana; orange grape';
const regex = /[,;\s]+/;
const fruits = text.split(regex);
console.log(fruits); // ['apple', 'banana', 'orange', 'grape']

常见应用

验证邮箱

javascript
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
console.log(emailRegex.test('user@example.com')); // true
console.log(emailRegex.test('invalid-email')); // false

验证手机号

javascript
const phoneRegex = /^1[3-9]\d{9}$/;
console.log(phoneRegex.test('13812345678')); // true
console.log(phoneRegex.test('12345678901')); // false

验证URL

javascript
const urlRegex = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/;
console.log(urlRegex.test('https://example.com')); // true
console.log(urlRegex.test('www.example.com')); // true

提取日期

javascript
const dateRegex = /(\d{4})-(\d{2})-(\d{2})/;
const text = '今天是2023-05-15';
const match = dateRegex.exec(text);
console.log(`年: ${match[1]}, 月: ${match[2]}, 日: ${match[3]}`);

替换敏感词

javascript
const sensitiveWords = ['敏感词1', '敏感词2'];
const text = '这是一个敏感词1和敏感词2的例子';
const regex = new RegExp(sensitiveWords.join('|'), 'gi');
const newText = text.replace(regex, '***');
console.log(newText); // '这是一个***和***的例子'

学习资源


继续学习:错误处理

最后更新:2026-02-08