javascript中正则表达式应用学习_match()

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
var s = "The rain in Spain falls mainly in the plain";
function MatchDemo(){
   var r, re;         // 声明变量。
  // var s = "The rain in Spain falls mainly in the plain";
   re = /ain/;    // 创建正则表达式模式。
   r = s.match(re);   // 尝试匹配搜索字符串。
   alert(r);
   if(r != null)alert(r.length);
   if(r != null)alert("the regular's input is : " + r.input);
   if(r != null)alert("the regular's index is : " + r.index);
   if(r != null)alert("the regular's lastIndex is : " + r.lastIndex);
   return(r);         // 返回第一次出现 "ain" 的地方。
}
function MatchDemo_i(){
   var r, re;         // 声明变量。
  //var s = "The rain in Spain falls mainly in the plain";
   re = /ain/i;    // 创建正则表达式模式。
   r = s.match(re);   // 尝试匹配搜索字符串。
   alert(r);
   if(r != null)alert(r.length);
   if(r != null)alert("the regular's input is : " + r.input);
   if(r != null)alert("the regular's index is : " + r.index);
   if(r != null)alert("the regular's lastIndex is : " + r.lastIndex);
   return(r);         // 返回第一次出现 "ain" 的地方。
}
function MatchDemo_g(){
   var r, re;         // 声明变量。
   //var s = "The rain in Spain falls mainly in the plain";
   re = /ain/g;    // 创建正则表达式模式。
   r = s.match(re);   // 尝试匹配搜索字符串。
   alert(r);
   if(r != null)alert(r.length);
   if(r != null)alert("the regular's input is : " + r.input);
   if(r != null)alert("the regular's index is : " + r.index);
   if(r != null)alert("the regular's lastIndex is : " + r.lastIndex);
   return(r);         // 返回第一次出现 "ain" 的地方。
}
function MatchDemo_ig(){
   var r, re;         // 声明变量。
  // var s = "The rain in Spain falls mainly in the plain";
   re = /ain/ig;    // 创建正则表达式模式。
   r = s.match(re);   // 尝试匹配搜索字符串。
   alert(r);
   if(r != null)alert(r.length);
   if(r != null)alert("the regular's input is : " + r.input);
   if(r != null)alert("the regular's index is : " + r.index);
   if(r != null)alert("the regular's lastIndex is : " + r.lastIndex);
   return(r);         // 返回第一次出现 "ain" 的地方。
}
function replaceStr(){
 var re=/ain/;
 s = s.replace(re,"TEST");
 alert("the string change to : " + s);
}
function replaceStr_i(){
 var re=/ain/i;
 s = s.replace(re,"TEST");
 alert("the string change to : " + s);
}
function replaceStr_g(){
 var re=/ain/g;
 s = s.replace(re,"TEST");
 alert("the string change to : " + s);
}
function replaceStr_ig(){
 var re=/ain/ig;
 s = s.replace(re,"TEST");
 alert("the string change to : " + s);
}
//-->
</SCRIPT>
字符串:The rain in Spain falls mainly in the plain
<p>
<input type="button" value="MatchDemo()" οnclick="javascript:MatchDemo()"/>
<input type="button" value="MatchDemo_i()" οnclick="javascript:MatchDemo_i()"/>
<input type="button" value="MatchDemo_g()" οnclick="javascript:MatchDemo_g()"/>
<input type="button" value="MatchDemo_ig()" οnclick="javascript:MatchDemo_ig()"/>
<input type="button" value="replaceStr()" οnclick="javascript:replaceStr()"/>
<input type="button" value="replaceStr_i()" οnclick="javascript:replaceStr_i()"/>
<input type="button" value="replaceStr_g()" οnclick="javascript:replaceStr_g()"/>
<input type="button" value="replaceStr_ig()" οnclick="javascript:replaceStr_ig()"/>
</BODY>
</HTML>

说明

如果 match 方法没有找到匹配,返回 null。如果找到匹配返回一个数组并且更新全局 RegExp 对象的属性以反映匹配结果。

match 方法返回的数组有三个属性:inputindexlastIndexInput 属性包含整个的被查找字符串。Index 属性包含了在整个被查找字符串中匹配的子字符串的位置。LastIndex 属性包含了最后一次匹配中最后一个字符的下一个位置。

如果没有设置全局标志 (g),数组的0元素包含整个匹配,而第 1 到 n 元素包含了匹配中曾出现过的任一个子匹配。这相当于没有设置全局标志的 exec 方法。如果设置了全局标志,元素0到n中包含所有匹配。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值