// 示例代码:使用正则表达式 match 方法
const str = "Hello, my email is example@example.com and my phone number is 123-456-7890.";
const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;
const phoneRegex = /\d{3}-\d{3}-\d{4}/g;
// 使用 match 方法查找字符串中的所有电子邮件地址
const emails = str.match(emailRegex);
console.log("匹配到的电子邮件地址:", emails); // 输出: ["example@example.com"]
// 使用 match 方法查找字符串中的所有电话号码
const phones = str.match(phoneRegex);
console.log("匹配到的电话号码:", phones); // 输出: ["123-456-7890"]
str 是包含电子邮件和电话号码的字符串。emailRegex 是一个用于匹配电子邮件地址的正则表达式。phoneRegex 是一个用于匹配电话号码(格式为 xxx-xxx-xxxx)的正则表达式。match 方法用于在字符串中查找与正则表达式匹配的所有子字符串,并返回一个数组。如果没有找到匹配项,则返回 null。g 标志表示全局匹配,即查找所有匹配项而不仅仅是第一个匹配项。上一篇:js map数据结构
下一篇:js .match
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站