常用正则表达式PHP中的应用及JAVAscript客户端中的应用
正则表达式用于字符串处理、表单验证等场合,实用高效。现将一些常用的表达式收集于此,以备不时之需。
匹配中文字符的正则表达式: [\u4e00-\u9fa5]:匹配中文还真是个头疼的事,有了这个表达式就好办了,从网上找的貌似验证不了,暂时可以用\w来验证中文。\w 匹配包括下划线的任何单词字元。等价于「[A-Za-z0-9_],暂且用来验证不包括这些字符。php中utf-8编码下用正则表达式匹配汉字的最终正确表达式——/^[\x{4e00}-\x{9fa5}]+$/u。
匹配双字节字符(包括汉字在内):[^\x00-\xff]:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
匹配空白行的正则表达式:\n\s*\r:可以用来删除空白行
匹配HTML标记的正则表达式:<(\S*?)[^>]*>.*?\1>|<.*? />:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
匹配首尾空白字符的正则表达式:^\s*|\s*$:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*:表单验证时很实用
匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*:网上流传的版本功能很有限,上面这个基本可以满足需求
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$:表单验证时很实用
匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7} :匹配形式如 0511-4405222 或 021-87888822
匹配几种格式的电话号码,像(010)88886666,或022-22334455,或02912345678:\(?0\d{2}[) -]?\d{8}
匹配腾讯QQ号:[1-9][0-9]{4,} :腾讯QQ号从10000开始
匹配中国邮政编码:[1-9]\d{5}(?!\d) :中国邮政编码为6位数字
匹配身份证:\d{15}|\d{18} :中国的身份证为15位或18位
匹配ip地址:\d+\.\d+\.\d+\.\d+ :提取ip地址时有用
匹配特定数字:
^[1-9]\d*$ //匹配正整数
^-[1-9]\d*$ //匹配负整数
^-?[1-9]\d*$ //匹配整数
^[1-9]\d*|0$ //匹配非负整数(正整数 + 0)
^-[1-9]\d*|0$ //匹配非正整数(负整数 + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //匹配正浮点数
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //匹配负浮点数
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //匹配浮点数
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //匹配非正浮点数(负浮点数 + 0)
正则表达式限制输入整数和小数string pattern = @"^\d{1,7}(?:\.\d{0,2}$|$)";
校验是否全由数字组成:^[0-9]{1,20}$
因为现在大量15,18等开头的手机号,所以电话及手机验证写成如下.
电话号码与手机号码同时验证:(^(\d{3,4}-)?\d{7,8})$|(1[0-9]{10})
评注:处理大量数据时有用,具体应用时注意修正
匹配特定字符串:
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
评注:最基本也是最常用的一些表达式
Javascript客户端对常用正则表达式的判断。
function chk(theForm){ if (theForm.name.value.replace(/(^\s*)|(\s*$)/g, "") == "" || theForm.name.value.replace(/[\u4e00-\u9fa5]/g, "")){ alert("姓名不能为空且必须为中文!"); theForm.name.focus(); return (false); } if (theForm.tel.value.replace(/(^\s*)|(\s*$)/g, "") == "" || theForm.tel.value.replace(/(^(\d{3,4}-)?\d{7,8})$|(1[0-9]{10})/g, "")){ alert("联系方式必须是座机或手机号码!"); theForm.tel.focus(); return (false); } if (theForm.qq.value.replace(/(^\s*)|(\s*$)/g, "") != ""){ if (theForm.qq.value.replace(/[1-9][0-9]{4,}/g, "")){ alert("QQ号码格式不正确!"); theForm.qq.focus(); return (false); } } if (theForm.email.value.replace(/(^\s*)|(\s*$)/g, "") != ""){ if (theForm.email.value.replace(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/g, "")){ alert("邮箱格式不正确!"); theForm.email.focus(); return (false); } } if (theForm.store.value.replace(/(^\s*)|(\s*$)/g, "") != ""){ if (theForm.store.value.replace(/^\d{1,4}(?:\.\d{0,4}$|$)/g, "")){ alert("店面面积整数位最多为4位,小数位最多为4位的数值型数据!"); theForm.store.focus(); return (false); } } if (theForm.info.value.replace(/(^\s*)|(\s*$)/g, "") == "" || theForm.name.value.replace(/^.{0,600}$/g, "")){ alert("代理详情必须在0-200个字符之间!"); theForm.info.focus(); return (false); } if (theForm.vercode.value.replace(/(^\s*)|(\s*$)/g, "") == "" || theForm.vercode.value.replace(/[\d]/g, "")){ alert("验证码不能为空且必须为数字!"); theForm.vercode.focus(); return (false); } } from的属性是:onSubmit="return chk(this)" runat="server" 效果体验: 评注:最基本也是最常用的Javascript客户端正则表达式的应用 PHP中正则表达式对字符的过滤,这里我写了一个类,需要用的时候直接用对象即可。 class check { function __construct(){ } function PHPSQL($str,$alert){ //您填写的信息有误 if (preg_match("/select|insert|update|delete|union|and|or|into|load_file|outfile|[\']|[\<]|[\>]|[\%]|[\&]|[\(]|[\)]|[\;]|[\+]|[\-]|[\[]|[\]]|[\{]|[\}]|[\$]|[\^]|[\?]/u" ,$str)) { echo "<script language=javascript>alert('$alert');window.history.go(-1);</script>"; } } function chinese($str,$alert){ //姓名不能为空且必须为中文! if (!preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str)){ echo "<script language=javascript>alert('$alert');window.history.go(-1);</script>"; } } function phone($str,$alert){ //联系方式必须是座机或手机号码! if (!preg_match("/(^(\d{3,4}-)?\d{7,8})$|(1[0-9]{10})/u",$str)) { echo "<script language=javascript>alert('$alert');window.history.go(-1);</script>"; } } function qq($str,$alert){ //QQ号码格式验证! if (!preg_match("/[1-9][0-9]{4,}/u",$str)) { echo "<script language=javascript>alert('$alert');window.history.go(-1);</script>"; } } function email($str,$alert){ //验证EMAIL if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/u",$str)) { echo "<script language=javascript>alert('$alert');window.history.go(-1);</script>"; } } function store($str,$alert){ //验证小数位 if (!preg_match("/^\d{1,4}(?:\.\d{0,4}$|$)/u",$str)) { echo "<script language=javascript>alert('$alert');window.history.go(-1);</script>"; } } function info($str,$alert){ //代理详情必须在0-200个字符之间! if (!preg_match("/^.{0,600}$/u",$str)) { echo "<script language=javascript>alert('$alert');window.history.go(-1);</script>"; } } } //使用方法: $check=new check(); //实例化 $check->PHPSQL($_POST[info],"非法字符,请检查后重新输入");
评注:可以用效过滤非法字符,至于非法字符,可按网站情况而定。