匹配英文
1 | var str = /[^a-zA-Z]/g; |
匹配中文
1 | var str = /[^\u4E00-\u9FA5]/g |
空格
1 | var str = /^ +| +$/g; |
手机号
1 | var str = /^1[34578][0-9]{9}$/; |
邮箱
1 | var str = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; |
身份证
1 | var preg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/; |
只允许输入数字
1 | var str = v.replace(/[^\d]/g,''); |
千分位分隔
1 | var str = v.replace(/\d{1,3}(?=(\d{3})+(.\d*)?$)/g, '$&,') |
筛选字符串 eg: ‘myname=nike&age=15’
1 | let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); |
筛选字符串 eg:
1 | let regex = /[t_]+[\d\,\*]+/; |
清除空格
1 | String.prototype.trim = function() { |
微信号
1 | const wechatRegex = /^[a-zA-Z_][-_a-zA-Z0-9]{5,19}$/; |
查找微信号 允许微信号 | qq号 | 电话号
const wechatRegexInput = /^(([a-zA-Z_][-_a-zA-Z0-9]{4,19}$)|([1-9]\d{4,10}$)|(1[3456789][0-9]{9}$))/;