8、正则表达式实用配方集锦
2025/9/23大约 5 分钟
正则表达式实用配方集锦
正则表达式是处理文本的强大工具,但有时很难记住或构建适用于特定任务的模式。本文收集了各种常见场景下的正则表达式配方,你可以将其视为一个实用的参考库。
数字和数量验证
整数
// 匹配整数(正负)
const integerRegex = /^-?\d+$/;
// 匹配正整数
const positiveIntegerRegex = /^\d+$/;
// 匹配指定范围内的整数(例如0-100)
const rangeIntegerRegex = /^\d{1,3}$/;
const validateRange = num => rangeIntegerRegex.test(num) && Number(num) <= 100;
浮点数
// 匹配浮点数(允许正负)
const floatRegex = /^-?\d+(\.\d+)?$/;
// 匹配特定精度的浮点数(如最多2位小数)
const precisionRegex = /^-?\d+(\.\d{1,2})?$/;
货币
// 匹配美元金额
const usdRegex = /^\$\d{1,3}(,\d{3})*(\.\d{2})?$/;
// 匹配中国人民币金额
const cnyRegex = /^¥\d+(\.\d{1,2})?$/;
// 通用货币格式(接受不同货币符号)
const currencyRegex = /^[\$€£¥]\d{1,3}(,\d{3})*(\.\d{2})?$/;
日期和时间
日期格式
// 匹配 YYYY-MM-DD 格式
const isoDateRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
// 匹配 MM/DD/YYYY 格式
const usDateRegex = /^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/;
// 匹配 DD.MM.YYYY 格式
const euDateRegex = /^(0[1-9]|[12]\d|3[01])\.(0[1-9]|1[0-2])\.\d{4}$/;
时间格式
// 匹配 24 小时制时间(HH:MM:SS)
const timeRegex = /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/;
// 匹配 12 小时制时间(hh:mm AM/PM)
const ampmRegex = /^(0?[1-9]|1[0-2]):([0-5]\d)\s?(AM|PM|am|pm)$/;
日期时间组合
// 匹配 ISO 日期时间格式(YYYY-MM-DDTHH:MM:SS)
const isoDateTimeRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/;
// 可选毫秒和时区
const fullIsoDateTimeRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T([01]\d|2[0-3]):([0-5]\d):([0-5]\d)(\.\d{1,3})?(Z|[+-][01]\d:[0-5]\d)?$/;
网络和通信
邮箱地址
// 基本邮箱验证(简单但适合大多数情况)
const basicEmailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// 更严格的邮箱验证
const strictEmailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
URL
// 基本 URL 验证(http/https)
const urlRegex = /^(https?:\/\/)?([\w\.-]+)\.([a-z\.]{2,6})([/\w\.-]*)*\/?$/;
// 更完整的 URL 验证
const fullUrlRegex = /^(https?:\/\/)?(www\.)?[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}(\/[^\s]*)?$/;
IP 地址
// IPv4 地址
const ipv4Regex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
// IPv6 地址(简化版)
const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,7}:$|^([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}$|^([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}$|^([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}$|^([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}$|^[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})$|^:((:[0-9a-fA-F]{1,4}){1,7}|:)$/;
电话号码
// 国际电话号码(E.164 格式)
const e164PhoneRegex = /^\+[1-9]\d{1,14}$/;
// 美国电话号码
const usPhoneRegex = /^(\+?1[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
// 中国大陆手机号
const cnPhoneRegex = /^1[3-9]\d{9}$/;
身份验证
密码强度
// 基本密码验证(至少8个字符)
const basicPasswordRegex = /^.{8,}$/;
// 中等强度密码(至少8个字符,包含大小写字母和数字)
const mediumPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;
// 高强度密码(至少8个字符,包含大小写字母、数字和特殊字符)
const strongPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*(),.?":{}|<>]).{8,}$/;
身份证号
// 中国大陆身份证号(18位)
const cnIdCardRegex = /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9X]$/;
// 美国社会安全号码
const ssnRegex = /^\d{3}-\d{2}-\d{4}$/;
代码和标记语言
HTML 标签
// 匹配 HTML 标签
const htmlTagRegex = /<\/?([a-z][a-z0-9]*)(?:[^>"']*|"[^"]*"|'[^']*')*>/ig;
// 提取 HTML 标签的属性
const htmlAttrRegex = /([a-z][a-z0-9]*)=("[^"]*"|'[^']*')/ig;
CSS 选择器
// 匹配 CSS ID 选择器
const cssIdRegex = /#[\w-]+/g;
// 匹配 CSS 类选择器
const cssClassRegex = /\.[\w-]+/g;
文件路径
// 匹配 Windows 文件路径
const windowsPathRegex = /^[a-zA-Z]:\\([^\\/:*?"<>|]+\\)*[^\\/:*?"<>|]*$/;
// 匹配 UNIX 文件路径
const unixPathRegex = /^\/([^/]+\/)*[^/]*$/;
文本处理
空白符处理
// 去除字符串首尾空白
const trimRegex = /^\s+|\s+$/g;
const trim = str => str.replace(trimRegex, '');
// 压缩连续空白为单个空格
const normalizeSpacesRegex = /\s+/g;
const normalizeSpaces = str => str.replace(normalizeSpacesRegex, ' ');
单词处理
// 提取单词
const wordsRegex = /\b\w+\b/g;
const extractWords = text => text.match(wordsRegex) || [];
// 计算单词数量
const countWords = text => (text.match(wordsRegex) || []).length;
驼峰命名与连字符命名转换
// 连字符转驼峰
const kebabToCamel = str => str.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
// 驼峰转连字符
const camelToKebab = str => str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
正则表达式使用技巧
提取捕获组所有匹配
// 提取所有匹配和捕获组
function extractAllMatches(regex, text) {
const results = [];
let match;
regex = new RegExp(regex, regex.flags.includes('g') ? regex.flags : regex.flags + 'g');
while ((match = regex.exec(text)) !== null) {
results.push(match.slice());
}
return results;
}
// 使用示例
const text = 'apple: $2.99, banana: $1.99, orange: $0.99';
const priceRegex = /(\w+):\s+\$(\d+\.\d{2})/g;
console.log(extractAllMatches(priceRegex, text));
// 输出:[['apple: $2.99', 'apple', '2.99'], ['banana: $1.99', 'banana', '1.99'], ['orange: $0.99', 'orange', '0.99']]
以上正则表达式配方集合涵盖了许多常见场景,可以作为你日常开发中的参考。请记住,尽管这些模式在大多数情况下都能工作,但可能需要根据特定需求进行调整。同时,某些复杂验证(如邮箱地址的完整RFC规范)可能需要更复杂的解决方案或辅助验证逻辑。