var text = "this has been a short summer";
var pattern = /(..)or(.)/g;
if (pattern.test(text)){
alert(RegExp.$1); //sh
alert(RegExp.$2); //t
}
如上代码意思为找到'or'字符串,$1为匹配处的前两位字符,$2为匹配处的后1位字符
var text = "this has been a short summer";
var pattern = /(.*)as(.*)or(.*)/g;
if (pattern.test(text)){
alert(RegExp.$1); //this h
alert(RegExp.$2); // been a sh
alert(RegExp.$3); //t summer
}
var text = "this has been a short summer";
var pattern = /(.)hort/g;
if (pattern.test(text)){
alert(RegExp.input); // this has been a short summer
alert(RegExp.leftContext);// this has been a
alert(RegExp.rightContext);// summer
alert(RegExp.lastMatch);// short
alert(RegExp.lastParen);// s
alert(RegExp.multiline);// false
}
- input属性返回原始字符串
- leftContext属性返回了单词short之前的字符串, 而rightContext返回了单词short之后的字符串
- lastMatch属性返回最近一次与整个正则表达式匹配的字符串,即short
- lastParen属性返回最近一次匹配的捕获组,即s
等同于
if (pattern.test(text)){
alert(RegExp.$_);// this has been a short summer
alert(RegExp["$`"]);// this has been a
alert(RegExp["$'"]);// summer
alert(RegExp["$&"]);// short
alert(RegExp["$+"]);// s
alert(RegExp["$*"]);// false
网友评论