每天一篇javascript学习小结(RegExp对象)
1、正则表达式test方法
vartext="cat,bat,sat,fat";
varpattern=/.at/;
if(pattern.test(text)){
alert("Thepatternwasmatched.");
}
2、正则的toString()方法
varpattern=newRegExp("\\[bc\\]at","gi");
alert(pattern.toString());///\[bc\]at/gi
alert(pattern.toLocaleString());///\[bc\]at/gi
3、RegExpConstructor(构造函数)Properties(属性)
vartext="thishasbeenashortsummer";
varpattern=/(.)hort/g;
/*
*Note:Operadoesn'tsupportinput,lastMatch,lastParen,ormultiline.
*InternetExplorerdoesn'tsupportmultiline.
*/
if(pattern.test(text)){
alert(RegExp.input);//thishasbeenashortsummer
alert(RegExp.leftContext);//thishasbeena
alert(RegExp.rightContext);//summer
alert(RegExp.lastMatch);//short
alert(RegExp.lastParen);//s
alert(RegExp.multiline);//false
}
input保存被搜索的字符串
index保存匹配的首字符的位置
lastIndex保存匹配的字符串下一个字符的位置
lastMatch保存匹配到的字符串
lastParen保存最后一个被匹配的字符串(最后一个括号内的内容)
leftContext保存匹配字符串左边的内容
rightContext保存匹配字符串右边的内容
$1~$9保存最开始的9个子匹配(括号中的内容)
vartext="thishasbeenashortsummer";
varpattern=/(.)hort/g;
/*
*Note:Operadoesn'tsupportshortpropertynames.
*InternetExplorerdoesn'tsupportmultiline.
*/
if(pattern.test(text)){
alert(RegExp.$_);//thishasbeenashortsummer
alert(RegExp["$`"]);//thishasbeena
alert(RegExp["$'"]);//summer
alert(RegExp["$&"]);//short
alert(RegExp["$+"]);//s
alert(RegExp["$*"]);//false
}
*分为长属性名和短属性名
*input$_最近一次要匹配的字符串
*lastMatch$&最近一次的匹配项
*lastParen$+最近一次匹配的捕获组
*leftContext$`input字符串中lastMatch之前的文本
*multiline$*布尔值,表示是否所有表达式都使用多行模式。
*rightContext$'input字符串中lastMatch之后的文本
4、正则$1.....$9
vartext="thishasbeenashortsummer";
varpattern=/(..)or(.)/g;
if(pattern.test(text)){
alert(RegExp.$1);//sh
alert(RegExp.$2);//t
}
每当产生一个带括号的成功匹配时,$1...$9属性的值就被修改。
可以在一个正则表达式模式中指定任意多个带括号的子匹配,但只能存储最新的九个。
5、RegExpexec()
vartext="momanddadandbaby"; varpattern=/mom(anddad(andbaby)?)?/gi; varmatches=pattern.exec(text); alert(matches.index);//0第一个被匹配到的位置 alert(matches.input);//"momanddadandbaby"匹配的原始字符串 alert(matches[0]);//"momanddadandbaby"匹配的第一个值 alert(matches[1]);//"anddadandbaby"匹配的第二个值 alert(matches[2]);//"andbaby"匹配的第三个值
vartext="cat,bat,sat,fat"; varpattern1=/.at/; varmatches=pattern1.exec(text); alert(matches.index);//0 alert(matches[0]);//"cat" alert(pattern1.lastIndex);//0 matches=pattern1.exec(text); alert(matches.index);//0 alert(matches[0]);//"cat" alert(pattern1.lastIndex);//0 varpattern2=/.at/g; varmatches=pattern2.exec(text); alert(matches.index);//0 alert(matches[0]);//"cat" alert(pattern2.lastIndex);//0 matches=pattern2.exec(text); alert(matches.index);//5 alert(matches[0]);//"bat" alert(pattern2.lastIndex);//0
6、RegExp实例属性
varpattern1=/\[bc\]at/i;
alert(pattern1.global);//false//是否设置全局查找
alert(pattern1.ignoreCase);//true是否忽略大小写
alert(pattern1.multiline);//false是否设置多行查找
alert(pattern1.lastIndex);//0一个整数,标示开始下一次匹配的字符位置。
alert(pattern1.source);//"\[bc\]at"正则表达式的源文本。
varpattern2=newRegExp("\\[bc\\]at","i");
alert(pattern2.global);//false
alert(pattern2.ignoreCase);//true
alert(pattern2.multiline);//false
alert(pattern2.lastIndex);//0
alert(pattern2.source);//"\[bc\]at"
以上就是今天的javascript学习小结,之后每天还会继续更新,希望大家继续关注。