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
%> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值