常用正则表达式

匹配中文字符的正则表达式: [\一-\龥]
匹配双字节字符(包括汉字在内):[^\x00-\xff]
匹配空行的正则表达式:\n[\s| ]*\r
匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/ 
匹配首尾空格的正则表达式:(^\s*)|(\s*$)(像vbscript那样的trim函数)
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
以下是例子:
利用正则表达式限制网页表单里的文本框输入内容:
用正则表达式限制只能输入中文:οnkeyup="value=value.replace(/[^\一-\龥]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\一-\龥]/g,''))"
1.用正则表达式限制只能输入全角字符: οnkeyup="value=value.replace(/[^\?-\?]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\?-\?]/g,''))"
2.用正则表达式限制只能输入数字:οnkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
3.用正则表达式限制只能输入数字和英文:οnkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

 

 

 

===================================

  • package com.geedao.util;  
  •  
  • /** 
  • * @author 卢向东 lxdhdgss@gmail.com 检验输入 
  • */ 
  • public class Regex {  
  •  
  •     /** 
  •      * 检查email输入是否正确 
  •      * 正确的书写格式为 username@domain 
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkEmail(String value, int length) {  
  •             return value.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")&&value.length()<=length;  
  •     }  
  •  
  •     /** 
  •      * 检查电话输入是否正确 
  •      * 正确格式 012-87654321、0123-87654321、0123-7654321 
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkTel(String value) {  
  •         return value.matches("\\d{4}-\\d{8}|\\d{4}-\\d{7}|\\d(3)-\\d(8)");    
  •     }  
  •  
  •     /** 
  •      * 检查手机输入是否正确 
  •      *  
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkMobile(String value) {  
  •         return value.matches("^[1][3,5]+\\d{9}");  
  •     }  
  •  
  •     /** 
  •      * 检查中文名输入是否正确 
  •      *  
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkChineseName(String value, int length) {  
  •         return value.matches("^[\u4e00-\u9fa5]+$")&&value.length()<=length;  
  •     }  
  •     /** 
  •      * 检查HTML中首尾空行或空格 
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkBlank(String value){  
  •         return value.matches("^\\s*|\\s*$");  
  •     }  
  •     /** 
  •      * 检查字符串是否含有HTML标签 
  •      * @param value 
  •      * @return 
  •      */ 
  •       
  •     public boolean checkHtmlTag(String value){  
  •         return value.matches("<(\\S*?)[^>]*>.*?</\\1>|<.*? />");  
  •     }  
  •     /** 
  •      * 检查URL是否合法 
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkURL(String value){  
  •         return value.matches("[a-zA-z]+://[^\\s]*");  
  •     }  
  •     /** 
  •      * 检查IP是否合法 
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkIP(String value){  
  •         return value.matches("\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}");  
  •     }  
  •     /** 
  •      * 检查ID是否合法,开头必须是大小写字母,其他位可以有大小写字符、数字、下划线 
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkID(String value){  
  •         return value.matches("[a-zA-Z][a-zA-Z0-9_]{4,15}$");  
  •     }  
  •     /** 
  •      * 检查QQ是否合法,必须是数字,且首位不能为0,最长15位 
  •      * @param value 
  •      * @return 
  •      */ 
  •       
  •     public boolean checkQQ(String value){  
  •         return value.matches("[1-9][0-9]{4,13}");  
  •     }  
  •     /** 
  •      * 检查邮编是否合法 
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkPostCode(String value){  
  •         return value.matches("[1-9]\\d{5}(?!\\d)");  
  •     }  
  •     /** 
  •      * 检查身份证是否合法,15位或18位 
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkIDCard(String value){  
  •         return value.matches("\\d{15}|\\d{18}");  
  •     }  
  •     /** 
  •      * 检查输入是否超出规定长度 
  •      *  
  •      * @param length 
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkLength(String value, int length) {  
  •         return ((value == null || "".equals(value.trim())) ? 0 : value.length()) <= length;  
  •     }  
  •  
  •     /** 
  •      * 检查是否为空字符串,空:true,不空:false 
  •      *  
  •      * @param value 
  •      * @return 
  •      */ 
  •     public boolean checkNull(String value) {  
  •         return value == null || "".equals(value.trim());  
  •     }  
  •  
  •  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值