Home » ★JavaScriptリファレンス

★JavaScriptリファレンス

RegExp.lastMatch …… 最後にマッチした文字列を返す

Internet Explorer
Google Chrome
Safari
Firefox
Opera
広告



RegExpオブジェクトのlastMatchプロパティは、最後にマッチした文字列を返します。

RegExpオブジェクトのlastMatchプロパティは、すでにウェブ標準から削除されています。
RegExp[‘$&’]と書いても同じ結果を得られるので、後者の書式で記述しても良いでしょう。

■構文・引数・戻り値

構文
RegExp.lastMatch
RegExp[‘$&’]
戻り値
最後にマッチした文字列

■使用例

HTML + JavaScriptソース

<script>
var sample = /Ball/g;
sample.test("BaseBall VolleyBall BasketBall");

document.write("<div>最後にマッチした文字列1: " + RegExp.lastMatch + "</div>");
document.write("<div>最後にマッチした文字列2: " + RegExp['$&'] + "</div>");

document.write("<div>最新のマッチした文字列の左側の文字列1: " + RegExp.leftContext + "</div>");
document.write("<div>最新のマッチした文字列の左側の文字列2: " + RegExp['$`'] + "</div>");

document.write("<div>最新のマッチした文字列の右側の文字列1: " + RegExp.rightContext + "</div>");
document.write("<div>最新のマッチした文字列の右側の文字列2: " + RegExp["$'"] + "</div>");
</script>

↓↓↓

ブラウザ上の表示

“);
document.write(“

最後にマッチした文字列2: ” + RegExp[‘$&’] + “

“);

document.write(“

最新のマッチした文字列の左側の文字列1: ” + RegExp.leftContext + “

“);
document.write(“

最新のマッチした文字列の左側の文字列2: ” + RegExp[‘$`’] + “

“);

document.write(“

最新のマッチした文字列の右側の文字列1: ” + RegExp.rightContext + “

“);
document.write(“

最新のマッチした文字列の右側の文字列2: ” + RegExp[“$'”] + “

“);

Lorem Ipsum is simply dummy text

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.