手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp编程>列表

正则表达式快速入门教程

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

  ReplaceTest = regEx.Replace(str1, replStr) ’ 作替换。
 End Function
 Response.write ReplaceTest("fox", "cat") & "<BR>" ’ 将 ’fox’ 替换为 ’cat’。
 Response.write ReplaceTest("(\S )(\s )(\S )", "$3$2$1") ’ 交换词对.
%>


  2、Test 方法

  对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。正则表达式搜索的实际模式是通过RegExp对象的Pattern属性来设置的。RegExp.Global属性对Test方法没有影响。

  如果找到了匹配的模式,Test方法返回True;否则返回False。下面的代码说明了Test 方法的用法。

<%
Function RegExpTest(patrn, strng)
 Dim regEx, retVal ’ 建立变量。
 Set regEx = New RegExp ’ 建立正则表达式。
 regEx.Pattern = patrn ’ 设置模式。
 regEx.IgnoreCase = False ’ 设置是否区分大小写。
 retVal = regEx.Test(strng) ’ 执行搜索测试。
 If retVal Then
  RegExpTest = "找到一个或多个匹配。"
 Else
  RegExpTest = "未找到匹配。"
 End If
End Function
Response.write RegExpTest("is.", "IS1 is2 IS3 is4")
%>


  3、Execute 方法

  对指定的字符串执行正则表达式搜索。正则表达式搜索的设计模式是通过 RegExp 对象的 Pattern 来设置的。

  Execute 方法返回一个 Matches 集合,其中包含了在 string 中找到的每一个匹配的 Match 对象。如果未找到匹配,Execute 将返回空的 Matches 集合。

  在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

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!