一文彻底弄清正则(?:x),x(?=y),(?<=y)x的区别

正则表达式中的 (?:x) 是非捕获型分组,它和普通的括号 (x) 在功能上类似,都可以将多个元素组合成一个整体,但 (?:x) 不会捕获匹配的内容,因此在匹配时不会生成捕获组。这在某些情况下可以提高匹配效率,并且在不需要捕获的情况下可以避免生成不必要的捕获组。
  1. 匹配用户名中的数字: 假设有一个用户名格式要求必须包含数字,但不允许数字出现在特定位置,可以使用 (?:\d) 来排除特定位置的数字。
const usernamePattern = /^[a-zA-Z]+(?:\d)[a-zA-Z\d]*$/;
const username1 = 'user123'; // 包含数字,合法
const username2 = 'user1name'; // 不包含数字,不合法
const username3 = '1username'; // 数字在特定位置,不合法

console.log(usernamePattern.test(username1)); // true
console.log(usernamePattern.test(username2)); // false
console.log(usernamePattern.test(username3)); // false

  1. 匹配 URL 中的查询参数: 有时候需要从 URL 中提取特定的查询参数值,但不需要捕获整个查询参数字符串,可以使用 (?:x) 来进行非捕获性匹配。
const url = 'https://example.com/page?param1=value1&param2=value2';
const paramPattern = /(?:param1=)([^&]+)/;

const match = url.match(paramPattern);
const paramValue = match ? match[1] : null;

console.log(paramValue); // 'value1'
  1. 匹配文本中的时间格式: 假设需要从文本中提取时间格式,但不需要捕获整个时间字符串,可以使用 (?:x) 来进行非捕获性匹配。
const text = 'Meeting at 3:30 PM';
const timePattern = /(?:\d{1,2}:\d{2} [AP]M)/;

const match = text.match(timePattern);
const time = match ? match[0] : null;

console.log(time); // '3:30 PM'
当你需要匹配一个字符串,该字符串后面紧跟着另一个特定的模式时,可以使用正则表达式中的 x(?=y) 结构,其中 x 表示要匹配的模式,而 y 则表示后置条件。这种结构称为正向先行断言。以下是三个常见的业务例子:
  1. 匹配包含特定单词的文件名: 假设你想要匹配包含特定单词的文件名,但不包括文件扩展名。例如,匹配文件名中包含 “report” 的文件,但不包括 “.txt” 扩展名。
const filenamePattern = /\b\w+(?=\.txt)/;
const filename1 = 'sales_report.txt'; // 匹配 "report"
const filename2 = 'annual_report.pdf'; // 不匹配

console.log(filename1.match(filenamePattern)[0]); // 'report'
console.log(filename2.match(filenamePattern)); // null

  1. 匹配包含特定格式的日期: 假设你想要匹配包含特定格式的日期,例如 “YYYY-MM-DD”,但不匹配日期之后的文本。
const text = 'Today is 2022-04-30, tomorrow will be 2022-05-01.';
const datePattern = /\d{4}-\d{2}-\d{2}(?=[,.])/g;

const matches = text.match(datePattern);
console.log(matches); // ['2022-04-30', '2022-05-01']
  1. 匹配包含特定格式的电话号码: 假设你想要匹配包含特定格式的电话号码,例如 “(XXX) XXX-XXXX”,但不匹配电话号码之后的文本。
const text = 'Contact us at (555) 123-4567 or email@example.com.';
const phonePattern = /\(\d{3}\) \d{3}-\d{4}(?=\s|$)/g;

const matches = text.match(phonePattern);
console.log(matches); // ['(555) 123-4567']
(?<=y) 是正则表达式中的后行断言,表示要匹配紧跟在特定模式(y)之后的内容(x)。这种结构通常用于需要在匹配模式之后验证特定条件的情况。以下是三个常见的业务例子:
  1. 匹配价格数字: 假设你有一段文本,其中包含了商品价格信息,价格格式为 “$XXX.XX”(美元和小数点后两位)。你想要匹配价格数字,但不包括美元符号。
const text = 'The price is $99.99, and it is on sale for $79.99.';
const pricePattern = /(?<=\$)\d+\.\d{2}/g;

const matches = text.match(pricePattern);
console.log(matches); // ['99.99', '79.99']
  1. 匹配特定前缀的单词: 假设你需要匹配文本中以特定前缀开头的单词,例如以 “user_” 开头的用户名。你想要匹配这些单词,但不包括前缀部分。
const text = 'The user_id is user_123, and the user_name is user_john_doe.';
const wordPattern = /(?<=\buser_)\w+/g;

const matches = text.match(wordPattern);
console.log(matches); // ['123', 'john_doe']
  1. 匹配特定格式的时间: 假设你有一段文本,其中包含了时间信息,时间格式为 “HH:MM”(24 小时制)。你想要匹配这些时间,但不包括分钟部分。
const text = 'The meeting starts at 09:30, and lunch break is at 12:00.';
const timePattern = /(?<=\b\d{2}:)\d{2}/g;

const matches = text.match(timePattern);
console.log(matches); // ['30', '00']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值