正則表達式(Regular Expression,在代碼中常簡寫為regex、regexp或RE)是計算機科學的一個概念。正則表達式使用單個字符串來描述、匹配一系列符合某個句法規則的字符串。在很多文本編輯器里,正則表達式通常被用來檢索、替換那些符合某個模式的文本。許多程序設計語言都支持利用正則表達式進行字符串操作。在很多文本編輯器里,正則表達式通常被用來檢索、替換那些符合某個模式的文本。
正則表達式用于字符串處理、表單驗證等場合,實用高效。現將一些常用的表達式收集于此,以備不時之需。
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
|
/** 驗證是否為EMAIL格式 */ public static final String EMAIL = "('')|(\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*)" ; /** 驗證電話號碼 */ public static final String TELEPHONE = "('')|(\\d{4}(-*)\\d{8}|\\d{4}(-*)\\d{7}|\\d{3}(-*)\\d{8}|\\d{3}(-*)\\d{7})" ; /** 驗證手機號碼 */ public static final String MOBILEPHONE = "1(3|5|8|7)\\d{9}" ; // "[1][3|5|8]+\\d{9}"; /** 驗證是否是電話或者手機號碼 */ public static final String TELEMOBILE = "^((\\d{3,4}?-|\\(\\d{3,4}\\))?\\d{8,11}$)|(^0{0,1}13[0-9]{9}$)" ; /** 是否全部為中文 */ public static final String CHINESECHAR = "^[\u4e00-\u9fa5]+$" ; /** 檢查字符串中是否還有HTML標簽 */ public static final String HTMLTAGHAS = "<(\\S*?)[^>]*>.*?</\\1>|<.*? />" ; /** 檢查URL是否合法 */ public static final String URL = "[a-zA-z]+://[^\\s]*" ; /** 檢查IP是否合法 */ public static final String IPADRESS = "\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}" ; /** 檢查QQ號是否合法 */ public static final String QQCODE = "[1-9][0-9]{4,13}" ; /** 檢查郵編是否合法 */ public static final String POSTCODE = "[1-9]\\d{5}(?!\\d)" ; /** 正整數 */ public static final String POSITIVE_INTEGER = "^[0-9]\\d*$" ; /** 正浮點數 */ public static final String POSITIVE_FLOAT = "^[1-9]\\d*.\\d*|0.\\d*[0-9]\\d*$" ; /** 整數或小數 */ public static final String POSITIVE_DOUBLE = "^[0-9]+(\\.[0-9]+)?$" ; /** 年月日 2012-1-1,2012/1/1,2012.1.1 */ public static final String DATE_YMD = "^\\d{4}(\\-|\\/|.)\\d{1,2}\\1\\d{1,2}$" ; /** 檢查身份證是否合法 驗證時請先驗證長度是否為15為或者18位 */ public static final String IDCARD = "\\d{6}(19|20)*[0-99]{2}(0[1-9]{1}|10|11|12)(0[1-9]{1}" + "|1[0-9]|2[0-9]|30|31)(\\w*)" ; /** 檢查護照是否合法 */ public static final String PASSPORT1 = "/^[a-zA-Z]{5,17}$/" ; public static final String PASSPORT2 = "/^[a-zA-Z0-9]{5,17}$/" ; /** 港澳通行證驗證 */ public static final String HKMAKAO = "/^[HMhm]{1}([0-9]{10}|[0-9]{8})$/" ; /** 臺灣通行證驗證 */ public static final String TAIWAN1 = " /^[0-9]{8}$/" ; public static final String TAIWAN2 = "/^[0-9]{10}$/" ; |
// 護照驗證
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
jQuery.validator.addMethod( "isPassport" , function (value, element, type) { if ($(type).val() === '2' ) { varre1 = /^[a-zA-Z]{5,17}$/; varre2 = /^[a-zA-Z0-9]{5,17}$/; returnthis.optional(element) || (re2.test(value)) || re1.test(value); } else { returntrue; } }, "護照格式不正確" ); |
// 港澳通行證驗證
1
2
3
4
5
6
7
8
9
10
11
12
13
|
jQuery.validator.addMethod( "isHKMacao" , function (value, element, type) { if ($(type).val() === '3' ) { varre = /^[HMhm]{1}([0-9]{10}|[0-9]{8})$/; returnthis.optional(element) || (re.test(value)); } else { returntrue; } }, "港澳通行證格式不正確" ); |
// 臺灣通行證驗證
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
jQuery.validator.addMethod( "isTaiwan" , function (value, element, type) { if ($(type).val() == "4" ) { varre1 = /^[0-9]{8}$/; varre2 = /^[0-9]{10}$/; returnthis.optional(element) || (re1.test(value)) || (re2.test(value)) } else { returntrue; } }, "臺灣通行證格式不正確" ); |
以上所述是小編給大家介紹的常用證件的正則表達式(大全),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/m0_37837382/article/details/65448339