正则表达式是 JavaScript 中处理字符串的强大工具,可以轻松地进行搜索、匹配和操作文本。在本博客文章中,我们将探讨 20 个实用的 JavaScript 正则表达式示例。
翻译本文的目的是想帮助开发者巩固正则基础,同时也想帮助对正则感兴趣的小伙伴,实现快速入门正则匹配。
1. 基本匹配
const pattern = /hello/;
const text = "Say hello to the world!";
console.log(pattern.test(text)); // true
console.log(text.match(pattern)) // hello
2. 不区分大小写的匹配
const pattern = /hello/i;
const text = "Say Hello to the world!";
console.log(pattern.test(text)); // true
console.log(text.match(pattern)) // Hello
3. 全局匹配
const pattern = /hello/g;
const text = "Hello, hello, hello!";
console.log(text.match(pattern)); // ["hello", "hello", "hello"]
4. 匹配任意内容的单个字符
const pattern = /h.llo/;
const text = "Say hxllo to the world!";
console.log(pattern.test(text)); // true
5. 匹配任意长度、任意内容字符(通配符)
const pattern = /h.*o/;
const text = "Say h3ll0 to the world!";
console.log(pattern.test(text)); // true
6. 匹配字符串的开始部分
const pattern = /^hello/;
const text = "hello world!";
console.log(pattern.test(text)); // true
7. 匹配字符串的结束部分
const pattern = /world!$/;
const text = "hello world!";
console.log(pattern.test(text)); // true
8. 匹配单词的边界
const pattern = /\bhello\b/;
const text = "Say a big hello!";
console.log(pattern.test(text)); // true
9. 匹配特定范围内的字符
const pattern = /[1-9a-zA-Z]/;
const text = "123abc456";
console.log(pattern.test(text)); // true
10. 匹配指定长度的字符
// \d表示匹配数字
const pattern = /\d{4}/;
const text = "Year: 2022";
text.match(pattern) //["2022"]
11. 匹配一个或多个字符
const pattern = /h.+o/;
const text = "hello!";
console.log(text.match(pattern)); // ["hello"]
12. 匹配0个或多个字符
const pattern = /h.*o/;
const text = "ho!";
console.log(text.match(pattern)); // ["ho"]
13. 匹配两种字符串中的任意一种
const pattern = /hello|world/;
const text = "Say hello and world!";
console.log(pattern.test(text)); // true
14. 捕获组(Capturing Groups)
const pattern = /(hello) (world)/;
const text = "hello world!";
const match = pattern.exec(text);
console.log(match[1]); // "hello"
console.log(match[2]); // "world"
15. 非捕获组(Non-Capturing Groups)
// (?:hello)是一个非捕获组,表示匹配 "hello" 但不捕获该分组
const pattern = /(?:hello) (world)/;
const text = "hello world!";
const match = pattern.exec(text);
console.log(match[1]); // "world"
16. 前瞻断言(Lookahead Assertions)
const pattern = /hello(?= world)/;
const text = "hello world!";
console.log(pattern.test(text)); // true
这段正则表达的意思是:只有hello后面跟着的是world才表示匹配成功,但是匹配到的内容还是hello。
17. 否定的前瞻断言(Negative Lookahead Assertions)
const pattern = /hello(?! world)/;
const text = "hello earth!";
console.log(pattern.test(text)); // true
这段正则表达的意思是:hello后面跟着的不是world,则匹配成功,,匹配到的内容是hello。
18. 后行断言(Lookbehind Assertions)
// 匹配以say开头的hello字符串
const pattern = /(?<=say )hello/;
const text = "say hello!";
console.log(pattern.test(text)); // true
19. 否定的后行断言(Negative Lookbehind Assertions)
// 匹配不是以say开头的hello字符串
const pattern = /(?<!say )hello/;
const text = "greet hello!";
console.log(pattern.test(text)); // true
20. 使用正则实现内容替换
const pattern = /hello/;
const text = "Say hello to the world!";
const newText = text.replace(pattern, "hi");
console.log(newText); // "Say hi to the world!"
如侵则删,原文地址