Skip to content

Instantly share code, notes, and snippets.

@luokailuo
Created April 27, 2016 02:43
Show Gist options
  • Save luokailuo/80cd4eecd130d558d5b3954c711dda75 to your computer and use it in GitHub Desktop.
Save luokailuo/80cd4eecd130d558d5b3954c711dda75 to your computer and use it in GitHub Desktop.
20个正则表达式
1.check password strength
//密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间
-----------------------
^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
-----------------------
2.check chinese
//字符串仅能是中文
-----------------------
^[\\u4e00-\\u9fa5]{0,}$
-----------------------
3.composition of the string by figures, 26 letters or underscore
-----------------------
^\\w+$
-----------------------
4.check email address
-----------------------
[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?
-----------------------
5.check ID number
15:
---------------------
^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$
---------------------
18:
---------------------
^[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)$
---------------------
6.check the date
//“yyyy-mm-dd“ 格式的日期校验,已考虑平闰年
-----------------------
^(?:(?!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)$
-----------------------
7.check the $/¥
//金额校验,精确到2位小数
-----------------------
^[0-9]+(.[0-9]{2})?$
-----------------------
8.check the cellphone number(china)
//13、15、18开头(可根据目前国内收集号扩展前两位开头号码)
-----------------------
^(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}$
-----------------------
9.check ie version
-----------------------
^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$
-----------------------
10.check the ip-v4
-----------------------
\\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
-----------------------
11.check the ip-v6
-----------------------
(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))
-----------------------
12.check the URL prefix
//应用开发中很多时候需要区分请求是HTTPS还是HTTP,通过下面的表达式可以取出一个url的前缀然后再逻辑判断
-----------------------
if (!s.match(/^[a-zA-Z]+:\\/\\//))
{
s = 'http://' + s;
}
----------------------
13.format URL link
//下面的这个表达式可以筛选出一段文本中的URL
----------------------
^(f|ht){1}(tp|tps):\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- ./?%&=]*)?
----------------------
14.check the file path and extension
//验证windows下文件路径和扩展名(下面的例子中为.txt文件)
----------------------
^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$
----------------------
15.get Color Hex Codes
//有时需要抽取网页中的颜色代码,可以使用下面的表达式
----------------------
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
----------------------
16.get web img
//假若你想提取网页中所有图片信息,可以利用下面的表达式
----------------------
\\< *[img][^\\>]*[src] *= *[\\"\\']{0,1}([^\\"\\'\\ >]*)
----------------------
17.get page link
//提取html中的超链接
----------------------
(<a\\s*(?!.*\\brel=)[^>]*)(href="https?:\\/\\/)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?', $follow_list).'))[^"]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)>
----------------------
18.find CSS properties
//可以搜索到相匹配的CSS属性
----------------------
^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;]{1}
----------------------
19.get page comments
//如果你需要移除HMTL中的注释,可以使用如下的表达式。
----------------------
<!--(.*?)-->
----------------------
20.matching HTML tags
//通过下面的表达式可以匹配出HTML中的标签属性
----------------------
<\\/?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|'.*?'|[\\^'">\\s]+))?)+\\s*|\\s*)\\/?>
----------------------
//Reference
//http://www.jianshu.com/p/e7bb97218946
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment