在 ECMAScript(JavaScript)中,正则表达式是一种强大的工具,用于在字符串中进行模式匹配、查找、替换等操作。以下是如何在 ECMAScript 中使用正则表达式的详细指南:
最常见的方式是通过字面量语法创建正则表达式:
const regex = /模式/标志;
g:全局匹配(在整个字符串中查找所有匹配项)i:忽略大小写m:多行模式示例:
// 匹配所有数字,全局且忽略大小写
const regex = /\d+/gi;
也可以通过 RegExp 构造函数创建正则表达式:
const regex = new RegExp('模式', '标志');
示例:
// 等同于上面的正则表达式
const regex = new RegExp('\\d+', 'gi');
注意: 当模式中包含特殊字符(如 ., *, ? 等)时,需要使用反斜杠 \ 进行转义,因此在字符串中使用双反斜杠 \\。
以下是一些常用的正则表达式模式及其含义:
. :匹配任意单个字符(除了换行符)^ :匹配字符串的开始$ :匹配字符串的结束* :匹配前面的子表达式零次或多次+ :匹配前面的子表达式一次或多次? :匹配前面的子表达式零次或一次{n} :匹配确定的 n 次{n,} :至少匹配 n 次{n,m} :最少匹配 n 次且最多 m 次[abc] :匹配方括号内的任意一个字符(a、b 或 c)[^abc] :匹配不在方括号内的任意一个字符(a|b) :匹配 a 或 b\d :匹配数字(等价于 [0-9])\D :匹配非数字\w :匹配字母、数字或下划线(等价于 [A-Za-z0-9_])\W :匹配非单词字符\s :匹配任何空白字符(包括空格、制表符、换行符等)\S :匹配任何非空白字符使用 test 方法可以测试一个字符串是否包含匹配正则表达式的子串:
const regex = /\d+/;
const str = "There are 123 apples.";
console.log(regex.test(str)); // 输出: true
使用 exec 方法可以在字符串中执行匹配操作,返回一个数组包含匹配结果,如果没有匹配则返回 null:
const regex = /\d+/g;
const str = "There are 123 apples and 456 oranges.";
let match;
while ((match = regex.exec(str)) !== null) {
console.log(`找到匹配: ${match[0]},位置: ${match.index}`);
}
// 输出:
// 找到匹配: 123,位置: 13
// 找到匹配: 456,位置: 31
字符串对象提供了多个方法用于执行正则表达式匹配:
String.prototype.match(regexp) :返回一个数组包含所有匹配结果,如果没有匹配则返回 null。如果正则表达式带有全局标志 g,则返回所有匹配的数组;否则返回包含详细信息的数组。
const regex = /\d+/g;
const str = "There are 123 apples and 456 oranges.";
const matches = str.match(regex);
console.log(matches); // 输出: ["123", "456"]
String.prototype.search(regexp) :返回第一个匹配项的索引,如果没有匹配则返回 -1。
const regex = /\d+/;
const str = "There are 123 apples.";
const index = str.search(regex);
console.log(index); // 输出: 13
String.prototype.replace(regexp, replacement) :使用指定的字符串或函数替换匹配的子串。
const regex = /\d+/g;
const str = "There are 123 apples and 456 oranges.";
const newStr = str.replace(regex, 'NUMBER');
console.log(newStr); // 输出: "There are NUMBER apples and NUMBER oranges."
String.prototype.split(regexp) :使用正则表达式作为分隔符拆分字符串,返回一个数组。
const regex = /, /;
const str = "apple, banana, cherry";
const fruits = str.split(regex);
console.log(fruits); // 输出: ["apple", "banana", "cherry"]
常用的修饰符有:
g (全局匹配):查找所有匹配项,而不是仅查找第一个。i (忽略大小写):在进行匹配时忽略字母的大小写。m (多行模式):使 ^ 和 $ 可以匹配每一行的开始和结束,而不仅仅是整个字符串的开始和结束。示例:
const regex = /^hello/i;
const str1 = "Hello world";
const str2 = "hello there";
console.log(regex.test(str1)); // 输出: true
console.log(regex.test(str2)); // 输出: true
正则表达式中的括号 () 用于定义捕获组,可以捕获匹配的子字符串,并在后续操作中引用。
const regex = /(hello) (world)/;
const str = "hello world";
const matches = regex.exec(str);
console.log(matches[1]); // 输出: "hello"
console.log(matches[2]); // 输出: "world"
使用 ?: 可以创建非捕获组,不会捕获匹配的内容,但可以应用量词。
const regex = /(?:hello) (world)/;
const str = "hello world";
const matches = regex.exec(str);
console.log(matches[1]); // 输出: "hello"
// 没有第二个捕获组
可以通过反向引用 \1, \2 等引用前面捕获的组。
const regex = /(\w+) (\w+)/;
const str = "hello world";
const matches = regex.exec(str);
console.log(matches[1]); // 输出: "hello"
console.log(matches[2]); // 输出: "world"
// 使用反向引用
const replacedStr = str.replace(regex, '$2 $1');
console.log(replacedStr); // 输出: "world hello"
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const email = "example@example.com";
console.log(emailRegex.test(email)); // 输出: true
const phoneRegex = /^\d{10}$/;
const phone = "1234567890";
console.log(phoneRegex.test(phone)); // 输出: true
const url = "https://www.example.com/path?query=123";
const hostnameRegex = /^(?:https?:\/\/)?([^\/]+)/i;
const match = url.match(hostnameRegex);
if (match) {
console.log(match[1]); // 输出: "www.example.com"
}
const text = "Visit https://www.example.com for more information.";
const linkRegex = /https?:\/\/[^\s]+/g;
const newText = text.replace(linkRegex, '链接');
console.log(newText); // 输出: "Visit 链接 for more information."
(?=...) :匹配后面跟着某个模式的字符串。(?!...) :匹配后面不跟着某个模式的字符串。(?<=...) :匹配前面是某个模式的字符串。(?<!...) :匹配前面不是某个模式的字符串。示例:
// 匹配后面跟着 "world" 的 "hello"
const regex = /hello(?= world)/;
console.log(regex.test("hello world")); // 输出: true
console.log(regex.test("hello there")); // 输出: false
使用 (?<name>...) 可以为捕获组命名,便于引用和理解。
const regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const dateStr = "2023-10-05";
const match = dateStr.match(regex);
if (match) {
console.log(match.groups.year); // 输出: "2023"
console.log(match.groups.month); // 输出: "10"
console.log(match.groups.day); // 输出: "05"
}
ES6 引入了 u 标志,用于支持 Unicode 字符的正则表达式。
// 匹配任意一个 Unicode 字母
const regex = /\p{L}/u;
const str = "Hello 你好 こんにちは";
const matches = str.match(regex);
console.log(matches); // 输出: ["H", "e", "l", "l", "o", "你", "好", "こ", "ん", "に", "ち", "は"]
正则表达式在 JavaScript 中是一个非常强大且灵活的工具,适用于各种字符串处理需求。通过掌握基本的正则表达式语法、方法和高级特性,可以高效地进行文本匹配、查找、替换等操作。建议在实际开发中多加练习,熟悉常用的模式和技巧,以提升编程效率。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。