<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>正则表达式验证</title>
<script language="javascript">
function check(){
var txt1=document.form1.expression.value; //获取用户输入的正则表达式
var txt2=document.form1.example.value; //获取用户输入的测试案例
var patrn = new RegExp(txt1); //创建了一个正则表达式对象
//var patrn=/\d+/; //用户自己创建正则表达式,用//开始和结束
if(patrn.exec(txt2)) //判断是否匹配,返回真或假
alert("验证成功");
else
alert("验证失败");
return false;
}
function copyTotxt1(num){
var expr=document.getElementById(num);
document.form1.expression.value=expr.firstChild.nodeValue;
}
function copyTotxt2(num){
var expr=document.getElementById(num);
document.form1.example.value=expr.firstChild.nodeValue;
}
</script>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
.txtlocation {
text-align: center;
}
-->
</style>
</head>
<body>
<form action="http://www.hao123.com" method="post" name="form1" onsubmit="return check()">
<table width="383" height="60" border="0" cellpadding="0" cellspacing="3">
<tr>
<td width="280" height="20" align="center"><input name="expression" type="text" id="expression" size="40" /></td>
<td width="94" height="20" align="center">请输入表达式</td>
</tr>
<tr>
<td height="20" align="center"><input name="example" type="text" id="example" size="40" /></td>
<td height="20" align="center"><input name="submit" type="submit" value="提交" /></td>
</tr>
<tr>
<td height="20" colspan="2" align="center"><table width="94%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="left"><a href="表达式学习.doc">表达式学习 </a><a href="正则表达式.CHM">帮助文档</a></div></td>
</tr>
</table></td>
</tr>
</table>
</form>
<table width="948" height="151" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" class="txtlocation">
<tr>
<td width="305" height="20" align="center" bgcolor="#CCCCCC">表达式</td>
<td width="263" align="center" bgcolor="#CCCCCC">案例</td>
<td width="376" align="center" bgcolor="#CCCCCC">说明</td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF"><div id="value_1_1" ondblclick="copyTotxt1(id)">^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$</div></td>
<td height="20" bgcolor="#FFFFFF"><div id="value_1_2" ondblclick="copyTotxt2(id)">liu-hong@163vip.com</div></td>
<td bgcolor="#FFFFFF">Email验证:可以实现Email验证,能够实现绝大部分功能</td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF"><div id="value_2_1" ondblclick="copyTotxt1(id)">^1[3,5]\d{9,9}$</div></td>
<td height="20" bgcolor="#FFFFFF"><div id="value_2_2" ondblclick="copyTotxt2(id)">13272365216</div></td>
<td bgcolor="#FFFFFF">手记证验证:手机前两位号码必须为13或者15</td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF"><div id="value_3_1" ondblclick="copyTotxt1(id)">^-?\d+$</div></td>
<td height="20" bgcolor="#FFFFFF"><div id="value_3_2" ondblclick="copyTotxt2(id)">验证为整数 ?出现0次或1次</div></td>
<td bgcolor="#FFFFFF">+少出现1次,相当于 {1,},^从字符串开始匹配,不匹配任何字符</td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF"><div id="value_4_1" ondblclick="copyTotxt1(id)">^[A-Za-z0-9]+$</div></td>
<td height="20" bgcolor="#FFFFFF"><div id="value_4_2" ondblclick="copyTotxt2(id)">txt_567</div></td>
<td bgcolor="#FFFFFF">由数字和26个英文字母组成的字符串</td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF"><div id="value_5_1" ondblclick="copyTotxt1(id)">^\d{17,17}[\d,x]$</div></td>
<td height="20" bgcolor="#FFFFFF"><div id="value_5_2" ondblclick="copyTotxt2(id)">431021180536721343</div></td>
<td bgcolor="#FFFFFF">验证居民18位身份证</td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF"><div id="value_6_1" ondblclick="copyTotxt1(id)">^[\u4e00-\u9fa5]([\u4e00-\u9fa5]|\s)*$</div></td>
<td height="20" bgcolor="#FFFFFF"><div id="value_6_2" ondblclick="copyTotxt2(id)">中国 您好 </div></td>
<td bgcolor="#FFFFFF">验证汉字 如(你好,你 好,你好)</td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF"><div id="value_7_1" ondblclick="copyTotxt1(id)">^[0-9]+(.[0-9]{2})?$</div></td>
<td height="20" bgcolor="#FFFFFF"><div id="value_7_2" ondblclick="copyTotxt2(id)">验证必须是整数,或者带两位小数的实数</div></td>
<td bgcolor="#FFFFFF"><p>?匹配表达式0次或者1次,相当于 {0,1}</p></td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF"><div id="value_8_1" ondblclick="copyTotxt1(id)">^[a-z,A-Z,_]\w{6,14}$</div></td>
<td height="20" bgcolor="#FFFFFF"><div id="value_8_2" ondblclick="copyTotxt2(id)">验证变量的声明,不能为特殊字符第一个不为数字</div></td>
<td bgcolor="#FFFFFF"><p>\w任意一个字母或数字或下划线,也就是 A~Z,a~z,0~9,_ 中任意一个</p></td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF"><div id="value_9_1" ondblclick="copyTotxt1(id)"></div></td>
<td height="20" bgcolor="#FFFFFF"><div id="value_9_2" ondblclick="copyTotxt2(id)"></div></td>
<td bgcolor="#FFFFFF"> </td>
</tr>
</table>
</body>
</html>