RegExp 是 JavaScript 中用于处理文本模式匹配(正则表达式)的内置对象。它可以用来执行文本搜索、替换、提取和验证等操作。通过使用正则表达式,您可以快速、灵活地处理字符串中的模式匹配。
1. RegExp 对象的创建
RegExp 对象有两种创建方式:
1.1 使用字面量
let regex = /pattern/;
1.2 使用 RegExp 构造函数
let regex = new RegExp('pattern');
构造函数可以接受两个参数:
- 第一个参数是模式(正则表达式)。
- 第二个参数是标志(如
g、i、m等,表示不同的匹配选项)。
2. RegExp 对象的常用方法
2.1 test()
test() 方法用于检查字符串是否匹配正则表达式。如果字符串匹配模式,则返回 true,否则返回 false。
let regex = /hello/;
console.log(regex.test('hello world')); // 输出:true
console.log(regex.test('world hello')); // 输出:true
console.log(regex.test('hi there')); // 输出:false
2.2 exec()
exec() 方法用于在字符串中执行搜索。如果匹配成功,返回一个数组,包含匹配的结果。如果没有找到匹配,返回 null。
let regex = /world/;
let result = regex.exec('hello world');
console.log(result); // 输出:["world", index: 6, input: "hello world", groups: undefined]
如果正则表达式带有全局标志 g,exec() 会返回多个匹配结果。
let regex = /o/g;
let result = regex.exec('hello world');
console.log(result); // 输出:["o", index: 4, input: "hello world", groups: undefined]
result = regex.exec('hello world');
console.log(result); // 输出:["o", index: 7, input: "hello world", groups: undefined]
2.3 match()
match() 方法用于检索字符串中是否有匹配的子字符串。如果找到匹配,它会返回匹配的结果;如果没有找到匹配,返回 null。
let str = 'hello world';
let result = str.match(/hello/);
console.log(result); // 输出:["hello"]
如果正则表达式使用了全局标志 g,则会返回所有匹配的结果:
let str = 'hello hello hello';
let result = str.match(/hello/g);
console.log(result); // 输出:["hello", "hello", "hello"]
2.4 replace()
replace() 方法用于将匹配的子字符串替换为指定的内容。如果使用正则表达式和带有全局标志 g,则会替换所有匹配的部分。
let str = 'hello world';
let result = str.replace(/world/, 'everyone');
console.log(result); // 输出:hello everyone
如果使用全局标志 g,则会替换所有匹配的部分:
let str = 'hello hello hello';
let result = str.replace(/hello/g, 'hi');
console.log(result); // 输出:hi hi hi
2.5 search()
search() 方法用于返回正则表达式匹配到的第一个位置。如果没有匹配到,返回 -1。
let str = 'hello world';
let result = str.search(/world/);
console.log(result); // 输出:6
2.6 split()
split() 方法根据正则表达式分割字符串并返回一个数组。
let str = 'apple,banana,orange';
let result = str.split(/,/);
console.log(result); // 输出:["apple", "banana", "orange"]
3. 正则表达式的常见标志(Flags)
正则表达式可以带有不同的标志,标志决定了正则表达式的匹配行为。
3.1 g(全局匹配)
g 标志表示全局匹配,意味着会找到所有匹配项,而不仅仅是第一个。
let regex = /o/g;
let result = 'hello world'.match(regex);
console.log(result); // 输出:["o", "o"]
3.2 i(忽略大小写)
i 标志表示忽略大小写进行匹配。
let regex = /hello/i;
console.log(regex.test('Hello')); // 输出:true
console.log(regex.test('HELLO')); // 输出:true
console.log(regex.test('HeLLo')); // 输出:true
3.3 m(多行匹配)
m 标志使得 ^ 和 $ 匹配行的开始和结束,而不仅仅是字符串的开始和结束。
let regex = /^hello/m;
console.log(regex.test('hello\nworld')); // 输出:true
3.4 s(dotAll模式)
s 标志使得 . 可以匹配换行符。默认情况下,. 不会匹配换行符。
let regex = /hello.world/s;
console.log(regex.test('hello\nworld')); // 输出:true
3.5 u(Unicode)
u 标志启用 Unicode 模式,允许处理一些特殊字符。
let regex = /\u{1F600}/u;
console.log(regex.test('😀')); // 输出:true
3.6 y(粘性匹配)
y 标志启用粘性匹配,意味着匹配只能从当前位置开始,而不是跳过前面的字符。
let regex = /hello/y;
console.log(regex.exec('hello world')); // 输出:["hello", index: 0, input: "hello world", groups: undefined]
console.log(regex.exec('world hello')); // 输出:null
4. 正则表达式的常见用法
4.1 验证邮箱地址
let regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
console.log(regex.test('test@example.com')); // 输出:true
console.log(regex.test('invalid-email')); // 输出:false
4.2 提取数字
let str = 'Price: $123.45';
let regex = /\d+\.\d+/;
let result = str.match(regex);
console.log(result); // 输出:["123.45"]
4.3 替换多个空格为单个空格
let str = 'This is a test';
let result = str.replace(/\s+/g, ' ');
console.log(result); // 输出:This is a test
4.4 去除字符串中的空白字符
let str = ' Hello World ';
let result = str.replace(/^\s+|\s+$/g, '');
console.log(result); // 输出:Hello World
5. 总结
JavaScript 的 RegExp 对象强大而灵活,是文本处理和字符串操作中不可或缺的工具。通过正则表达式,您可以快速有效地匹配、替换、验证和提取字符串中的信息。掌握正则表达式的语法和使用方法,将大大提升您的开发效率。更多详细内容请关注其他相关文章。