Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

常用正则表达式 #18

Open
oliver1204 opened this issue Jan 10, 2017 · 0 comments
Open

常用正则表达式 #18

oliver1204 opened this issue Jan 10, 2017 · 0 comments

Comments

@oliver1204
Copy link
Owner

oliver1204 commented Jan 10, 2017

1. 校验基本日期格式

let reg = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/       

2. 校验密码强度

密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间。

let reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$/

3. 校验中文

字符串仅能是中文。

let reg = /^[\u4e00-\u9fa5]{0,}$/

4.由数字、26个英文字母或下划线组成的字符串

let reg = /^\w+$/

5. 校验E-Mail 地址

同密码一样,下面是E-mail地址合规性的正则检查语句。

let reg = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/

6. 校验身份证号码

下面是身份证号码的正则校验。15 或 18位。

6.1 15 位
let reg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/
6.2 18 位
let reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/

7. “yyyy-mm-dd“ 格式的日期校验,已考虑平闰年。

let reg = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$
/

8. 校验金额

金额校验,精确到2位小数。

let reg = /^[0-9]+(.[0-9]{2})?$/

9. 下面是国内 13、15、18开头的手机号正则表达式。

(可根据目前国内收集号扩展前两位开头号码)

let reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/

10. 判断IE的版本

IE目前还没被完全取代,很多页面还是需要做版本兼容,下面是IE版本检查的表达式。

let reg = /^.*MSIE [5-8](?:\.[0-9]+)?(?!.*Trident\/[5-9]\.0).*$/

11. 校验IP-v4地址

IP4 正则语句。

let reg = /\b(?:(?: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]?)\b/

12. 检查URL的前缀

应用开发中很多时候需要区分请求是HTTPS还是HTTP,通过下面的表达式可以取出一个url的前缀然后再逻辑判断。

if (!s.match(/^[a-zA-Z]+:\/\/)) {
    s = 'http://' + s;
}

13. 提取URL链接

下面的这个表达式可以筛选出一段文本中的URL。

let reg = /^(f|ht){1}(tp|tps):\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/

14. 提取网页图片

假若你想提取网页中所有图片信息,可以利用下面的表达式。

let reg = /\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\'\ >]*)/

15. 替换URL的某个字段

${document.domain.replace(/^h5\./, 'restapi.')}`

其他人总结

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant