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 方法返回的数组有三个属性:input、index和lastIndex。Input 属性包含整个的被查找字符串。Index 属性包含了在整个被查找字符串中匹配的子字符串的位置。LastIndex 属性包含了最后一次匹配中最后一个字符的下一个位置。
如果没有设置全局标志 (g),数组的0元素包含整个匹配,而第 1 到 n 元素包含了匹配中曾出现过的任一个子匹配。这相当于没有设置全局标志的 exec 方法。如果设置了全局标志,元素0到n中包含所有匹配。
<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 方法返回的数组有三个属性:input、index和lastIndex。Input 属性包含整个的被查找字符串。Index 属性包含了在整个被查找字符串中匹配的子字符串的位置。LastIndex 属性包含了最后一次匹配中最后一个字符的下一个位置。
如果没有设置全局标志 (g),数组的0元素包含整个匹配,而第 1 到 n 元素包含了匹配中曾出现过的任一个子匹配。这相当于没有设置全局标志的 exec 方法。如果设置了全局标志,元素0到n中包含所有匹配。
相关文章推荐
- javascript中正则表达式应用学习_test()
- JavaScript:学习笔记(3)——正则表达式的应用
- javascript中正则表达式应用学习_Split()
- javascript中正则表达式应用学习_search ()
- JavaScript学习之正则表达式
- javascript的正则表达式学习资料复习
- javascript学习笔记(八)正则表达式
- JavaScript 学习笔记 六 正则表达式《一》
- 正则表达式学习笔记004--连字符和范围描述符的认识与应用
- JavaScript正则表达式校验与递归函数实际应用实例解析
- 《JavaScript 语言精粹》 学习笔记 —— 第七章 正则表达式
- JavaScript 学习笔记 之正则表达式
- javascript 正则表达式应用 收集
- 正则表达式在javascript中的应用
- JavaScript中的正则表达式学习笔记
- 【java学习】正则表达式的应用
- 深入浅出的javascript的正则表达式学习教程
- JavaScript学习笔记(七) JSON和正则表达式字面量
- JavaScript学习总结(八)正则表达式
- 【学习】Javascript正则表达式——基础知识小结