理解RegExp对象
真正有用的属性是 lastIndex, 它可以告诉你正则表达式在某个字符串中停止之前,查找了多远。
RegExp的实例有一些开发人员可以使用的属性.
global - Boolean值,表示g(全局属性)是否已经设置
ignoreCase - Boolean值,表示i(忽略大小写)是否已设置
lastIndex - 整数,代表下次匹配将会从那个字符位置开始(只有使用test()或exec()函数才会填入,否则为0)
multiline - Boolean值,表示m(多行模式选项)是否已设置
source - 正则表达式的源字符串形式,
global - Boolean值,表示g(全局属性)是否已经设置
ignoreCase - Boolean值,表示i(忽略大小写)是否已设置
lastIndex - 整数,代表下次匹配将会从那个字符位置开始(只有使用test()或exec()函数才会填入,否则为0)
multiline - Boolean值,表示m(多行模式选项)是否已设置
source - 正则表达式的源字符串形式,
例如
// 表达式/[ba]*/ 的source 将返回 "[ba]*"
var reTest =/[ba]*/i;
alert(reTest.global); //output false;
alert(reTest.ignoreCase); //output true;
alert(reTest.multiline); //output false
alert(reTest.source); //output [ba]*
// 表达式/[ba]*/ 的source 将返回 "[ba]*"
var reTest =/[ba]*/i;
alert(reTest.global); //output false;
alert(reTest.ignoreCase); //output true;
alert(reTest.multiline); //output false
alert(reTest.source); //output [ba]*
真正有用的属性是 lastIndex, 它可以告诉你正则表达式在某个字符串中停止之前,查找了多远。
var sToMatch = "bbq is short for barbecure";
var regB = /b/g;
regB.exec(sToMatch);
alert(regB.lastIndex); //1 ,第一个字符是 b ,他的位置是0,所以lastIndex的值为 1
regB.exec(sToMatch);
alert(regB.lastIndex); //2 第二个字符是 b 他的位置是 1,所以lastIndex的值为 2
regB.exec(sToMatch);
alert(regB.lastIndex); //18 第三个字符是 b 他的位置是 17,所以lastIndex的值为 18
regB.exec(sToMatch);
alert(regB.lastIndex); //21 第四个字符是 b,他的位置是 20,所以lastIndex的值为 21
var regB = /b/g;
regB.exec(sToMatch);
alert(regB.lastIndex); //1 ,第一个字符是 b ,他的位置是0,所以lastIndex的值为 1
regB.exec(sToMatch);
alert(regB.lastIndex); //2 第二个字符是 b 他的位置是 1,所以lastIndex的值为 2
regB.exec(sToMatch);
alert(regB.lastIndex); //18 第三个字符是 b 他的位置是 17,所以lastIndex的值为 18
regB.exec(sToMatch);
alert(regB.lastIndex); //21 第四个字符是 b,他的位置是 20,所以lastIndex的值为 21
静态属性
----------------------------------------------------------------------------------------------------
长名 短名 描述
----------------------------------------------------------------------------------------------------
input $_ 用户最后匹配的字符串(传递给exec()和test()方法的字符串)
lastMatch $& 最后匹配的字符
lastParen $+ 最后匹配的分组
leftContext $` 在上次匹配前的子串
multiline $* 用于指定是否所有的表达式都使用多行模式的布尔值
rightContext $' 在上次匹配之后的子串
----------------------------------------------------------------------------------------------------
这些属性可以告诉你刚使用exec()和test()完成的匹配的一些特定信息。
----------------------------------------------------------------------------------------------------
长名 短名 描述
----------------------------------------------------------------------------------------------------
input $_ 用户最后匹配的字符串(传递给exec()和test()方法的字符串)
lastMatch $& 最后匹配的字符
lastParen $+ 最后匹配的分组
leftContext $` 在上次匹配前的子串
multiline $* 用于指定是否所有的表达式都使用多行模式的布尔值
rightContext $' 在上次匹配之后的子串
----------------------------------------------------------------------------------------------------
这些属性可以告诉你刚使用exec()和test()完成的匹配的一些特定信息。
var sToMatch ="this has been a short,short summer";
var reShort = /(s)hort/g;
reShort.test(sToMatch);
alert(RegExp.input); //output "this has been a short,short summer"
alert(RegExp.leftContext ); //output "this has been a " 包含第一个实例"short"之前的所有字符
alert(RegExp.rightContext); //output ",short summer" 包含第一个实例"short"只有所有的字符
alert(RegExp.lastMatch); //output "short" 包含最后匹配整个正则表达式的字符串
alert(RegExp.lastParen); //output "s" 包含最后匹配的分组
var reShort = /(s)hort/g;
reShort.test(sToMatch);
alert(RegExp.input); //output "this has been a short,short summer"
alert(RegExp.leftContext ); //output "this has been a " 包含第一个实例"short"之前的所有字符
alert(RegExp.rightContext); //output ",short summer" 包含第一个实例"short"只有所有的字符
alert(RegExp.lastMatch); //output "short" 包含最后匹配整个正则表达式的字符串
alert(RegExp.lastParen); //output "s" 包含最后匹配的分组
浙公网安备 33010602011771号