正則表達式(Regular Expression,在代碼中常簡寫為regex、regexp或RE)是計算機科學的一個概念。正則表達式使用單個字符串來描述、匹配一系列符合某個句法規則的字符串。在很多文本編輯器里,正則表達式通常被用來檢索、替換那些符合某個模式的文本。許多程序設計語言都支持利用正則表達式進行字符串操作。在很多文本編輯器里,正則表達式通常被用來檢索、替換那些符合某個模式的文本。
正則表達式用于字符串處理、表單驗證等場合,實用高效?,F將一些常用的表達式收集于此,以備不時之需。
/** 驗證是否為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}$/";
// 護照驗證
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;
}
},"護照格式不正確");
// 港澳通行證驗證
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;
}
},"港澳通行證格式不正確");
// 臺灣通行證驗證
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;
}
},"臺灣通行證格式不正確");
以上所述是小編給大家介紹的常用證件的正則表達式(大全),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
您可能感興趣的文章:- 只能輸入正整數的正則表達式及常用的正則表達式
- java基于正則表達式實現時間日期的常用判斷操作實例
- 前端常用正則表達式匯總
- 用戶名、密碼等15個常用的js正則表達式
- 正則表達式同時匹配中英文及常用正則表達式
- JS常用正則表達式及驗證時間的正則表達式
- 正則表達式常用用法匯總
- java正則表達式四種常用的處理方式(匹配、分割、替代、獲?。?/li>
- 常用正則表達式大全(金錢,非負整數,正整數,郵箱,手機號碼)