1、手机号检验
/^[1][3,4,5,6,7,8,9][0-9]{9}$/
2、身份证校验
/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
3、邮箱
/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
4、URL 校验
/^((https?|ftp|file|http?):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
5、IPV4校验
const ipv4Reg = /^(?:(?: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]?)$/
const ipv4Str1 = '122.12.56.65'
console.log(ipv4Reg.test(ipv4Str1)) // true
const ipv4Str2 = '122.12.56.655'
console.log(ipv4Reg.test(ipv4Str2)) // false
6、16进制颜色校验
const color16Reg = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
console.log(color16Reg.test('#fff')) // true
console.log(color16Reg.test('#123456')) // false
7、整数校验
const intReg = /^[-+]?\d*$/
console.log(intReg.test(12345)) // true
console.log(intReg.test(12345.1)) // false
8、保留n位小数
function checkFloat(n) {
return new RegExp(`^([1-9]+[\d]*(.[0-9]{1,${n}})?)$`)
}
// 保留2位小数
const floatReg = checkFloat(2)
console.log(floatReg.test(1234.5)) // true
console.log(floatReg.test(1234.55)) // true
console.log(floatReg.test(1234.555)) // false
9、邮政编码
/^\d{6}$/
10、车牌号校验
/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/
console.log(carNoReg.test('粤A12345')) // true
console.log(carNoReg.test('广东A12345')) // false
11、只能输入英文、数字、不可以输入中文
/[^\w\/]/ig
12、限制只能是英文
/[^a-zA-Z]/g
13、限制是英文和中文
/^[\u4E00-\u9FA5A-Za-z]+$/g
14、只能输入数字、字母、下划线
/[^(\-)\w\.\/]/ig
15、是否包含中文
/[\u4E00-\u9FA5]/
16、密码强度校验
说明:必须包含字母,数字,特殊字符,至少8个字符,最多30个字符
/(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}/
17、文件名扩展匹配
function checkFileName (arr) {
arr = arr.map(name => `.${name}`).join('|')
return new RegExp(`(${arr})$`)
}
const filenameReg = checkFileName(['jpg', 'png', 'txt'])
const filename1 = 'sunshine.jpg'
console.log(filenameReg.test(filename1)) // true
const filename2 = 'sunshine.png'
console.log(filenameReg.test(filename2)) // true
const filename3 = 'sunshine.txt'
console.log(filenameReg.test(filename3)) // true
const filename4 = 'sunshine.md'
console.log(filenameReg.test(filename4)) // false
18、匹配html中img的src
const imgReg = /<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/ig
const htmlStr = '<div></div><img src="sunshine.png" /><img src="sunshine111.png" />'
console.log(imgReg.exec(htmlStr))
// [
// '<img src="sunshine.png" />',
// 'sunshine.png',
// index: 11,
// input: '<div></div><img src="sunshine.png" /><img src="sunshine111.png" />',
// groups: undefined
// ]
console.log(imgReg.exec(htmlStr))
// [
// '<img src="sunshine111.png" />',
// 'sunshine111.png',
// index: 37,
// input: '<div></div><img src="sunshine.png" /><img src="sunshine111.png" />',
// groups: undefined
// ]
19、匹配html中的行内样式
const styleReg = /style="[^=>]*"([(\s+\w+=)|>])/g
const htmlStr = '<div style="background:#000;"><span style="color:#fff"></span></div>'
console.log(styleReg.exec(htmlStr))
// [
// 'style="background:#000;">',
// '>',
// index: 5,
// input: '<div style="background:#000;"><span style="color:#fff"></span></div>',
// groups: undefined
// ] console.log(styleReg.exec(htmlStr))
// [ // 'style="color:#fff">',
// '>',
// index: 36,
// input: '<div style="background:#000;"><span style="color:#fff"></span></div>',
// groups: undefined
// ]
20、驼峰转下划线
const toLowerLine = str =>str.replace(/[A-Z]/g, match=>"_"+match.toLowerCase());
console.log(toLowerLine('mainBox')) // main_box
21、下划线转驼峰
const toCamel = str =>str.replace(/([^_])(?:_+([^_]))/g, (_,p1, p2)=>p1+p2.toUpperCase());