-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsRegExp.txt
377 lines (251 loc) · 10.8 KB
/
jsRegExp.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
手机号码正则表达式验证
function checkPhone(){
var phone = document.getElementById('phone').value;
if(!(/^1[34578]\d{9}$/.test(phone))){
alert("手机号码有误,请重填");
return false;
}
}
或者:
function checkPhone(){
var phone = document.getElementById('phone').value;
if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
alert("手机号码有误,请重填");
return false;
}
}
身份证正则:
//身份证正则表达式(15位)
isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
//身份证正则表达式(18位)
isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;
身份证正则合并:(^\d{15}$)|(^\d{17}([0-9]|X)$)
其他:
提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
提取信息中的图片链接:(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+)
提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}
提取信息中的中国邮政编码:[1-9]{1}(\d+){5}
提取信息中的中国身份证号码:\d{18}|\d{15}
提取信息中的整数:\d+
提取信息中的浮点数(即小数):(-?\d*)\.?\d+
提取信息中的任何数字 :(-?\d*)(\.\d+)?
提取信息中的中文字符串:[\u4e00-\u9fa5]*
提取信息中的双字节字符串 (汉字):[^\x00-\xff]*
1.正确表达式
"^\\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+)?$" //浮点数
2、使用方法
var r = /^\+?[1-9][0-9]*$/; //正整数
String str = "123";
boolean flag=r.test(str);
如果判断为正整数,则flag为true
3、JS整数相加
首先保证输入的都是数字
nText1=parseFloat(document.all.text1.value);
nText2=parseFloat(document.all.text2.value);
nSum=nText1+nText2
4.总结
Require : /.+/,
Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
Phone : /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/,
Mobile : /^((\(\d{3}\))|(\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+$/,
Zip : /^[1-9]\d{5}$/,
QQ : /^[1-9]\d{4,8}$/,
Integer : /^[-\+]?\d+$/,
Double : /^[-\+]?\d+(\.\d+)?$/,
Float: /^[-\+]?\d+(\.\d+)?$/,
Float2: /^(0|[1-9]\d{0,3})(\.\d{0,2})?$/,
English : /^[A-Za-z]+$/,
Chinese : /^[\u0391-\uFFE5]+$/,
DateTime:/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))(\s\d{1,2}:\d{1,2}:\d{1,2})?$/,
UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
IsSafe : function(str){return !this.UnSafe.test(str);},
SafeString : "this.IsSafe(value)",
Limit : "this.limit(value.length,getAttribute('min'), getAttribute('max'))",
LimitB : "this.limit(this.LenB(value), getAttribute('min'), getAttribute('max'))",
Date : "this.IsDate(value, getAttribute('min'), getAttribute('format'))",
Repeat : "value == document.getElementsByName(getAttribute('to'))[0].value",
Range : "getAttribute('min') < value && value < getAttribute('max')",
Compare : "this.compare(value,getAttribute('operator'),getAttribute('to'))",
Custom : "this.Exec(value, getAttribute('regexp'))",
Group : "this.MustChecked(getAttribute('name'), getAttribute('min'), getAttribute('max'))",
详细说明-几种基本数字验证:
只能输入数字
表达式 ^[0-9]*$
描述 匹配任意个数字
匹配的例子 12345678,1234567
不匹配的例子 E,
只能输入某个区间数字
表达式 ^[12-15]$
描述 匹配某个区间的数字
匹配的例子 12,13,14,15
不匹配的例子
只能输入0和非0打头的数字
表达式 ^(0|[1-9][0-9]*)$
描述 可以为0,第一个数字不能为0,数字中可以有0
匹配的例子 12,10,101,100
不匹配的例子 01,
只能输入实数
表达式 ^[-+]?\d+(\.\d+)?$
描述 匹配实数
匹配的例子 18,+3.14,-9.90
不匹配的例子 .6,33s,67-99
只能输入n位小数的正实数
表达式 ^[0-9]+(.[0-9]{n})?$以^[0-9]+(.[0-9]{2})?$为例
描述 匹配n位小数的正实数
匹配的例子 2.22
不匹配的例子 2.222,-2.22,
只能输入m-n位小数的正实数
表达式 ^[0-9]+(.[0-9]{m,n})?$以^[0-9]+(.[0-9]{1,2})?$为例
描述 匹配m到n位小数的正实数
匹配的例子 2.22,2.2
不匹配的例子 2.222,-2.2222,
只能输入非0的正整数
表达式 ^\+?[1-9][0-9]*$
描述 匹配非0的正整数
匹配的例子 2,23,234
不匹配的例子 0,-4,
只能输入非0的负整数
表达式 ^\-[1-9][0-9]*$
描述 匹配非0的负整数
匹配的例子 -2,-23,-234
不匹配的例子 0,4,
只能输入n个字符
表达式 ^.{n}$ 以^.{4}$为例
描述 匹配n个字符,注意汉字只算1个字符
匹配的例子 1234,12we,123清,清清月儿
不匹配的例子 0,123,123www,
只能输入英文字符
表达式 ^.[A-Za-z]+$为例
描述 匹配英文字符,大小写任意
匹配的例子 Asp,WWW,
不匹配的例子 0,123,123www,
只能输入大写英文字符
表达式 ^.[A-Z]+$为例
描述 匹配英文大写字符
匹配的例子 NET,WWW,
不匹配的例子 0,123,123www,
只能输入小写英文字符
表达式 ^.[a-z]+$为例
描述 匹配英文大写字符
匹配的例子 asp,csdn
不匹配的例子 0,NET,WWW,
只能输入英文字符+数字
表达式 ^.[A-Za-z0-9]+$为例
描述 匹配英文字符+数字
匹配的例子 1Asp,W1W1W,
不匹配的例子 0,123,123,www,
只能输入英文字符/数字/下划线
表达式 ^\w+$为例
描述 匹配英文字符或数字或下划线
匹配的例子 1Asp,WWW,12,1_w
不匹配的例子 3#,2-4,w#$,
密码举例
表达式 ^.[a-zA-Z] \w{m,n}$
描述 匹配英文字符开头的m-n位字符且只能数字字母或下划线
匹配的例子
不匹配的例子
验证首字母大写
表达式 \b[^\Wa-z0-9_][^\WA-Z0-9_]*\b
描述 首字母只能大写
匹配的例子 Asp,Net
不匹配的例子
验证网址(带?id=中文)VS.NET2005无此功能
表达式 ^http:\/\/([\w-]+(\.[\w-]+)+(\/[\w- .\/\?%&=\u4e00-\u9fa5]*)?)?$
描述 验证带?id=中文
匹配的例子 ,
http://blog.csdn.net/?id=清清月儿
不匹配的例子
验证汉字
表达式 ^[\u4e00-\u9fa5]{0,}$
描述 只能汉字
匹配的例子 清清月儿
不匹配的例子
验证QQ号
表达式 [0-9]{5,9}
描述 5-9位的QQ号
匹配的例子 10000,123456
不匹配的例子 10000w,
验证电子邮件(验证MSN号一样)
表达式 \w+([-+.´]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
描述 注意MSN用非hotmail.com邮箱也可以
匹配的例子 aaa@msn.com
不匹配的例子 111@1.
验证身份证号(粗验,最好服务器端调类库再细验证)
表达式 ^[1-9]([0-9]{16}|[0-9]{13})[xX0-9]$
描述
匹配的例子 15或者18位的身份证号,支持带X的
不匹配的例子
验证手机号(包含159,不包含小灵通)
表达式 ^13[0-9]{1}[0-9]{8}|^15[9]{1}[0-9]{8}
描述 包含159的手机号130-139
匹配的例子 139XXXXXXXX
不匹配的例子 140XXXXXXXX,
验证电话号码号(很复杂,VS.NET2005给的是错的)
表达式(不完美 ) 方案一 ((\(\d{3}\)|\d{3}-)|(\(\d{4}\)|\d{4}-))?(\d{8}|\d{7})
方案二 (^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$) 支持手机号但也不完美
描述 上海:02112345678 3+8位
上海:021-12345678
上海:(021)-12345678
上海:(021)12345678
郑州:03711234567 4+7位
杭州:057112345678 4+8位
还有带上分机号,国家码的情况
由于情况非常复杂所以不建议前台做100%验证,到目前为止似乎也没有谁能写一个包含所有的类型 ,其实有很多情况本身就是矛盾的。
如果谁有更好 的验证电话的请留言
匹配的例子
不匹配的例子
验证护照
表达式 (P\d{7})|G\d{8})
描述 验证P+7个数字和G+8个数字
匹配的例子
不匹配的例子
验证IP
表达式 ^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$
描述 验证IP
匹配的例子 192.168.0.1 222.234.1.4
不匹配的例子
验证域
表达式 ^[a-zA-Z0-9]+([a-zA-Z0-9\-\.]+)?\.(com|org|net|cn|com.cn|edu.cn|grv.cn|)$
描述 验证域
匹配的例子 csdn.net baidu.com it.com.cn
不匹配的例子 192.168.0.1
验证信用卡
表达式 ^((?:4\d{3})|(?:5[1-5]\d{2})|(?:6011)|(?:3[68]\d{2})|(?:30[012345]\d))[ -]?(\d{4})[ -]?(\d{4})[ -]?(\d{4}|3[4,7]\d{13})$
描述 验证VISA卡,万事达卡,Discover卡,美国运通卡
匹配的例子
不匹配的例子
验证ISBN 国际标准书号
表达式 ^(\d[- ]*){9}[\dxX]$
描述 验证ISBN国际标准书号
匹配的例子 7-111-19947-2
不匹配的例子
验证GUID 全球唯一标识符
表达式 ^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}$
描述 格式8-4-4-4-12
匹配的例子 2064d355-c0b9-41d8-9ef7-9d8b26524751
不匹配的例子
验证文件路径和扩展名
表达式 ^([a-zA-Z]\:|\\)\\([^\\]+\\)*[^\/:*?"<>|]+\.txt(l)?$
描述 检查路径和文件扩展名
匹配的例子 E:\mo.txt
不匹配的例子 E:\ , mo.doc, E:\mo.doc ,
验证Html颜色值
表达式 ^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$
描述 检查颜色取值
匹配的例子 #FF0000
不匹配的例子