快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

ASP中正则表达式的应用-2

三、JavaScript中正则表达式的使用
  在JavaScript 1.2版以后,JavaScript也支持正则表达式。
  1、replace
  replace在一个字符串中通过正则表达式查找替换相应的内容。replace并不改变原来的字符串,只是重新生成了一个新的字符串。假如需要执行全局查找或忽略大小写,那么在正则表达式的最后添加g和i。
例:
<SCRIPT>
re = /apples/gi;
str = "Apples are round, and apples are juicy.";
newstr=str.replace(re, "oranges");
document.write(newstr)
</SCRIPT>
结果是:"oranges are round, and oranges are juicy."
例:
<SCRIPT>
str = "Twas the night before Xmas...";
newstr=str.replace(/xmas/i, "Christmas");
document.write(newstr)
</SCRIPT>
结果是:"Twas the night before Christmas..."
例:
<SCRIPT>
re = /(w+)s(w+)/;str = "John Smith";
newstr = str.replace(re, "$2, $1");
document.write(newstr)
</SCRIPT>
结果是:"Smith, John".
  2、search
search通过正则表达式查找相应的字符串,只是判定有无匹配的字符串。假如查找成功,search返回匹配串的位置,否则返回-1。
search(regexp)
<SCRIPT>
function testinput(re, str){
if (str.search(re) != -1)
midstring = " contains ";
else
midstring = " does not contain ";
document.write (str + midstring + re.source);
}
testinput(/^[1-9]/i,"123")
</SCRIPT>
  3、match
  match方式执行全局查找,查找结果存放在一个数组里。
例一:
<SCRIPT>
str = "For more information, see Chapter 3.4.5.1";
re = /(chapter d+(.d)*)/i;
found = str.match(re);
document.write(found);
</SCRIPT>
显示结果:Chapter 3.4.5.1,Chapter 3.4.5.1,.1
例二:
<SCRIPT>
str = "abcDdcba";
newArray = str.match(/d/gi);
document.write(newArray);
</SCRIPT>
显示结果D, d.

  四、示例
1 、判定数字的准确性
<%@ Language=VBScript %>
<script language="javascript" runat="server">
function isNumeric(strNumber) {
return (strNumber.search(/^(-|+)?d+(.d+)?$/) != -1);
}
function isUnsignedNumeric(strNumber) {
return (strNumber.search(/^d+(.d+)?$/) != -1);
}
function isInteger(strInteger) {
return (strInteger.search(/^(-|+)?d+$/) != -1);
}
function isUnsignedInteger(strInteger) {
return (strInteger.search(/^d+$/) != -1);
}
</script>
<HTML>
<BODY>
<b>判定数字的准确性</b>
<%
Dim strTemp
strTemp = CStr(Request.Form("inputstring"))
If strTemp = "" Then strTemp = "0"
%>
<TABLE BORDER="1" CELLPADDING="4" CELLSPACING="2">
<TR>
<TD ALIGN="right"><B>原始字符串</B></TD>
<TD><%= strTemp %></TD>
</TR>
<TR>
<TD ALIGN="right"><B>数字</B></TD>
<TD><%=isNumeric(strTemp)%></TD>
</TR>
<TR>
<TD ALIGN="right"><B>非负数字</B></TD>
<TD><%=isUnsignedNumeric(strTemp)%></TD>
</TR>
<TR>
<TD ALIGN="right"><B>整数</B></TD>
<TD><%=isInteger(strTemp)%></TD>
</TR>
<TR>
<TD ALIGN="right"><B>非负整数()</B></TD>
<TD><%=isUnsignedInteger(strTemp)%></TD>
</TR>
</TABLE>
<FORM ACTION="<%=Request.ServerVariables("SCRIPT_NAME")%>" METHOD="post">
请输入一个数字:<BR>
<INPUT TYPE="text" NAME="inputstring" SIZE="50"></INPUT><BR>
<INPUT TYPE="submit" Value="提交"></INPUT><BR>
</FORM>
</BODY>
</HTML>
2、判定Email地址的准确性
<%
Function isemail(strng)
isemail = false
Dim regEx, Match
Set regEx = New RegExp
regEx.Pattern = "^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$"
regEx.IgnoreCase = True
Set Match = regEx.Execute(strng)
if match.count then isemail= true
End Function
%>

  五、总结
  上面我们介绍了正则表达式的基本概念,以及在VBScript和JavaScript中如何使用正则表达式,同时,通过一些实例让大家有了感性的熟悉。正则表达式的应用范围很广,能为大家解决很多实际中的问题。本文介绍的内容只是一些初步的知识,还有很多语法规则需要大家继承学习,在实践中发现问题,解决问题。
返回类别: 教程
上一教程: EVENT对象详解
下一教程: 打印出全部服务器变量的方式

您可以阅读与"ASP中正则表达式的应用-2"相关的教程:
· ASP中正则表达式的应用-1
· ASP正则表达式在UBB论坛中的应用
· 正则表达式在UBB论坛中的应用
· 正则表达式在网页处理中的应用四则
· 在ASP中利用“正则表达式” 对象实现UBB风格的论坛
    微笑服务 优质保证 索取样品