From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/regexp/lastindex/index.html | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/regexp/lastindex/index.html (limited to 'files/ja/web/javascript/reference/global_objects/regexp/lastindex') diff --git a/files/ja/web/javascript/reference/global_objects/regexp/lastindex/index.html b/files/ja/web/javascript/reference/global_objects/regexp/lastindex/index.html new file mode 100644 index 0000000000..d0b1eceda8 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/lastindex/index.html @@ -0,0 +1,89 @@ +--- +title: RegExp.lastIndex +slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex +tags: + - JavaScript + - Property + - Reference + - RegExp + - Regular Expression +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex +--- +
{{JSRef}}
+ +

lastIndex は正規表現インスタンスの読み書き可能なプロパティで、次の一致を開始する位置を指定します。

+ +
{{EmbedInteractiveExample("pages/js/regexp-lastindex.html")}}
+ +
{{js_property_attributes(1, 0, 0)}}
+ +

構文

+ +
regExpObj.lastIndex
+ +

解説

+ +

このプロパティは、正規表現インスタンスがグローバル検索を示すために g フラグを使用した場合、または粘着的検索を示すために y フラグを使用した場合にのみ設定されます。以下の規則が適用されます。

+ + + +

+ +

lastIndex の使用

+ +

例えば、以下の連続した処理を考えてみてください。:

+ +
var re = /(hi)?/g;
+
+ +

空文字列に一致します。

+ +
console.log(re.exec('hi'));
+console.log(re.lastIndex);
+
+ +

lastIndex が 2 になり["hi", "hi"] が返ります。

+ +
console.log(re.exec('hi'));
+console.log(re.lastIndex);
+
+ +

ゼロ番目の要素が一致した文字列なので、 ["", undefined] という空配列が返ります。この場合、 lastIndex が 2 であったときに (そして 2 のままである)、 hi の長さが 2 であるので、空文字列になります。

+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-properties-of-regexp-instances', 'RegExp.lastIndex')}}
+ +

ブラウザーの互換性

+ +
+ + +

{{Compat("javascript.builtins.RegExp.lastIndex")}}

+
+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf