-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex_lib.txt
58 lines (56 loc) · 5.27 KB
/
regex_lib.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$
只能输入数字:”^[0-9]*$”
只能输入n位的数字:”^\d{n}$”
只能输入至少n位的数字:”^\d{n,}$”
只能输入m~n位的数字:”^\d{m,n}$”
只能输入零和非零开头的数字:”^(0|[1-9][0-9]*)$”。
只能输入有两位小数的正实数:”^[0-9]+(.[0-9]{2})?$”。
只能输入有1~3位小数的正实数:”^[0-9]+(.[0-9]{1,3})?$”。
只能输入非零的正整数:”^\+?[1-9][0-9]*$”。
只能输入非零的负整数:”^\-[1-9][]0-9″*$。
只能输入长度为3的字符:”^.{3}$”。
只能输入由26个英文字母组成的字符串:”^[A-Za-z]+$”。
只能输入由26个大写英文字母组成的字符串:”^[A-Z]+$”。
只能输入由26个小写英文字母组成的字符串:”^[a-z]+$”。
只能输入由数字和26个英文字母组成的字符串:”^[A-Za-z0-9]+$”。
只能输入由数字、26个英文字母或者下划线组成的字符串:”^\w+$”。
验证用户密码:”^[a-zA-Z]\w{5,17}$”
正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。验证是否含有^%&’,;=?$\”等字符:”[^%&',;=?$\x22]+”。
只能输入汉字:”^[\u4e00-\u9fa5]{0,}$”
验证Email地址:”^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$”。
验证InternetURL:”^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$”。
验证电话号码:”^(\d3,4−)|\d3.4−)?\d7,8$”正确格式为:”XXX−XXXXXXX”、”XXXX−XXXXXXXX”、”XXX−XXXXXXX”、”XXX−XXXXXXXX”、”XXXXXXX”和”XXXXXXXX”。验证身份证号(15位或18位数字):”(\d15|\d18)$”。验证一年的12个月:”(0?[1−9]|1[0−2])$”正确格式为:”01''〜”09''和”1''〜”12''。验证一个月的31天:”((0?[1−9])|((1|2)[0−9])|30|31)$”正确格式为;”01''〜”09''和”1''〜”31''。匹配中文字符的正则表达式:[\u4e00−\u9fa5]匹配双字节字符(包括汉字在内):[\x00−\xff]应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)String.prototype.len=function()returnthis.replace(/[\x00−\xff]/g,”aa”).length;匹配空行的正则表达式:\n[\s|]∗\r匹配html标签的正则表达式:<(.∗)>(.∗)<\/(.∗)>|<(.∗)\/>匹配首尾空格的正则表达式:(\s∗)|(\s∗$)应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:String.prototype.trim=function()returnthis.replace(/(\s∗)|(\s∗$)/g,“”);利用正则表达式分解和转换IP地址:下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的Javascript程序:functionIP2V(ip)re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g//匹配IP地址的正则表达式if(re.test(ip))returnRegExp.$1∗Math.pow(255,3))+RegExp.$2∗Math.pow(255,2))+RegExp.$3∗255+RegExp.$4∗1elsethrownewError(“NotavalidIPaddress!”)不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:varip=”10.100.20.168''ip=ip.split(“.”)alert(“IP值是:”+(ip[0]∗255∗255∗255+ip[1]∗255∗255+ip[2]∗255+ip[3]∗1))匹配Email地址的正则表达式:\w+([−+.]\w+)∗@\w+([−.]\w+)∗\.\w+([−.]\w+)∗匹配网址URL的正则表达式:http://([\w−]+\.)+[\w−]+(/[\w−./?)|(\d{3}\-))?(0\d2,3|0\d{2,3}-)?[1-9]\d{6,7}$/
Mobile : /^((\d3)|(\d{3}\-))?13\d{9}$/
Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\"\"])*$/
IdCard : /^\d{15}(\d{2}[A-Za-z0-9])?$/
Currency : /^\d+(\.\d+)?$/
Number : /^\d+$/
Code : /^[1-9]\d{5}$/
QQ : /^[1-9]\d{4,8}$/
Integer : /^[-\+]?\d+$/
Double : /^[-\+]?\d+(\.\d+)?$/
English : /^[A-Za-z]+$/
Chinese : /^[\u0391-\uFFE5]+$/
UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.
\{\}<>\?\\\/\’\”]*)|.{0,5})$|\s/
PassWord :^[\\w]{6,12}$
ZipCode : ^[\\d]{6}/^(\+\d+ )?(\d+ )?[\d ]+$/;
//这个是国际通用的电话号码判断/^(1[0-2]\d|\d{1,2})$/;
//这个是年龄的判断/^\d+\.\d{2}$/;
//这个是判断输入的是否为货币值
应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)String.prototype.len=function(){return this.replace([^\x00-\xff]/g,”aa”).length;}
应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, “”);}
匹配空行的正则表达式:\n[\s| ]*\r
匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/
匹配首尾空格的正则表达式:(^\s*)|(\s*$)
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?^\d+$ //
匹配非负整数(正整数 + 0)^[0-9]*[1-9][0-9]*$ //
匹配正整数^((-\d+)|(0+))$ //
匹配非正整数(负整数 + 0)^-[0-9]*[1-9][0-9]*$ //
匹配负整数^-?\d+$ //
匹配整数^\d+(\.\d+)?$ //
匹配非负浮点数(正浮点数 + 0)^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ //
匹配正浮点数^((-\d+(\.\d+)?)|(0+(\.0+)?))$ //
匹配非正浮点数(负浮点数 + 0)^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ //
匹配负浮点数^(-?\d+)(\.\d+)?$