正規表現最期に一致した文字列と、その前後の文字列を取得
- 最期に一致した文字列と、その前後の文字列を取得〔lastMatchプロパティ/lastParenプロパティ/leftContextプロパティ/rightContextプロパティ〕
最期に一致した文字列と、その前後の文字列を取得
lastMatchプロパティ/lastParenプロパティ/leftContextプロパティ/rightContextプロパティ
unknown
プロパティ | 文法 | 説明 |
---|---|---|
lastMatchプロパティ | RegExp.lastMatch | 最期に一致した文字列を取得 |
lastParenプロパティ | RegExp.lastParen | 最期に一致したグループの文字列を取得 |
leftContextプロパティ | RegExp.leftContext | 最期に一致した文字列より前の文字列を取得 |
rightContextプロパティ | RegExp.rightContext | 最期に一致した文字列より後の文字列を取得 |
<script type="text/javascript">
/**
* 文字列中に含まれる単語を全文検索し、
* 最期に一致する文字列とその前後の文字列を取得
*/
var obj=RegExp(/\w+/g);
var str="Welcome to My Homepage!";
var res=str.match(obj);
with(document){
write('全文検索の結果='+res+'<br \/>\n');
write('最期に一致した文字列='+RegExp.lastMatch+'<br \/>\n');
write('最期に一致したグループの文字列='+RegExp.lastParen+'<br \/>\n');
write('最期に一致した文字列より前の文字列='+RegExp.leftContext+'<br \/>\n');
write('最期に一致した文字列より後の文字列='+RegExp.rightContext+'<br \/>\n');
}
</script>