您的位置:首页 > Web前端 > JavaScript

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

2004-09-29 14:14 846 查看
<!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()" onclick="javascript:MatchDemo()"/>
<input type="button" value="MatchDemo_i()" onclick="javascript:MatchDemo_i()"/>
<input type="button" value="MatchDemo_g()" onclick="javascript:MatchDemo_g()"/>
<input type="button" value="MatchDemo_ig()" onclick="javascript:MatchDemo_ig()"/>
<input type="button" value="replaceStr()" onclick="javascript:replaceStr()"/>
<input type="button" value="replaceStr_i()" onclick="javascript:replaceStr_i()"/>
<input type="button" value="replaceStr_g()" onclick="javascript:replaceStr_g()"/>
<input type="button" value="replaceStr_ig()" onclick="javascript:replaceStr_ig()"/>
</BODY>
</HTML>

说明

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

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

如果没有设置全局标志 (g),数组的0元素包含整个匹配,而第 1 到 n 元素包含了匹配中曾出现过的任一个子匹配。这相当于没有设置全局标志的 exec 方法。如果设置了全局标志,元素0到n中包含所有匹配。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: