常用正则表达式都有哪些,用代码写出来
时间: 2024-06-01 21:09:48 浏览: 25
常用的正则表达式包括:
1. 匹配手机号:/^[1][3-9][0-9]{9}$/
2. 匹配邮箱:/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
3. 匹配身份证号:/^\d{17}[\d|x]|\d{15}$/
4. 匹配URL:/^(http|https):\/\/([\w.]+\/?)\S*$/
5. 匹配IP地址:/^\d+\.\d+\.\d+\.\d+$/
代码实现:
1. 匹配手机号
const reg = /^[1][3-9][0-9]{9}$/;
const phone = '13812345678';
console.log(reg.test(phone)); // true
2. 匹配邮箱
const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
const email = 'test@example.com';
console.log(reg.test(email)); // true
3. 匹配身份证号
const reg = /^\d{17}[\d|x]|\d{15}$/;
const idCard = '110101199001011234';
console.log(reg.test(idCard)); // true
4. 匹配URL
const reg = /^(http|https):\/\/([\w.]+\/?)\S*$/;
const url = 'https://www.example.com';
console.log(reg.test(url)); // true
5. 匹配IP地址
const reg = /^\d+\.\d+\.\d+\.\d+$/;
const ipAddress = '192.168.0.1';
console.log(reg.test(ipAddress)); // true