liuzemin

正则表达式1

·replace使用:字符串.replace(正则表达式, 新字符);
·正则表达式的两种写法:① "笨蛋" ② /笨蛋/
·正则表达式的参数:① g:全局替换 ② i:忽略大小写
·match方法:把匹配到的字符存放在数组之中,使用:字符串.match(正则表达式)
·test方法:判断正则表达式是否匹配字符串,使用:正则表达式.test(字符串)

<!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" type="text/javascript">
function xianshi()
{
// 获取文本域的内容
var con = document.getElementById("content").value;
// 定义正则表达式
var reg = /^\d{6}$/g;   // 以六位数字开头,以六位数字结尾
if(reg.test(con))
{
   alert("验证通过!");
}
else
{
   alert("验证失败,只能输入6位数字!");
}
//var found = con.match(reg);
// 替换字符串
//con = con.replace(reg, "欢迎");
// 把获取的内容显示在span标签里
//document.getElementById("show").innerHTML = found;
}
</script>
</head>

<body>   
<form id="form1" name="form1" method="post" action="">
<p>请输入内容:
    <textarea name="content" id="content" cols="45" rows="5"></textarea>
</p>
<p>      
    <input type="button" name="button" id="button" value="提交" onclick="xianshi();" />
</p>
</form>
<span id="show"></span>
</body>
</html>

posted on 2009-03-08 21:37  liuzemin  阅读(119)  评论(0)    收藏  举报