js常用的正则表达式
1.匹配Email地址:
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
2.匹配URL:
const urlRegex = /^(https?:\/\/)?([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(:[0-9]+)?(\/[^\s]*)?$/;
3.匹配日期(YYYY-MM-DD):
const dateRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
4.匹配手机号码(简化版):
const phoneRegex = /^\d{11}$/;
5.匹配IP地址(IPv4):
const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
6.匹配数字:
const numberRegex = /^\d+$/;
7.匹配字母和数字:
const alphanumericRegex = /^[a-zA-Z0-9]+$/;
8.匹配至少包含一个大写字母、一个小写字母和一个数字的密码:
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;
9.匹配HTML标签:
const htmlTagRegex = /<[^>]+>/g;
10.匹配多个重复字符(例如,连续的重复字母或数字):
const repeatedCharRegex = /(.)\1+/g;
11.匹配只包含字母(大小写不限)和空格的字符串:
const lettersAndSpacesRegex = /^[a-zA-Z\s]+$/;