Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
---
title: RegExp.prototype.exec()
short-title: exec()
slug: Web/JavaScript/Reference/Global_Objects/RegExp/exec
l10n:
sourceCommit: 8421c0cd94fa5aa237c833ac6d24885edbc7d721
sourceCommit: cd22b9f18cf2450c0cc488379b8b780f0f343397
---

{{JSRef}}

**`exec()`** は {{jsxref("RegExp")}} インスタンスのメソッドで、指定された文字列の中でこの正規表現と一致するものを検索し、その結果の配列、または [`null`](/ja/docs/Web/JavaScript/Reference/Operators/null) を返します。

{{InteractiveExample("JavaScript デモ: RegExp.prototype.exec()")}}

```js interactive-example
const regex1 = RegExp("foo*", "g");
const str1 = "table football, foosball";
let array1;

while ((array1 = regex1.exec(str1)) !== null) {
console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`);
// Expected output: "Found foo. Next starts at 9."
// Expected output: "Found foo. Next starts at 19."
const regex = /fo+/g;
const str = "table football, foosball";
let array;

while ((array = regex.exec(str)) !== null) {
console.log(`Found ${array[0]}. Next starts at ${regex.lastIndex}.`);
// 予想される結果: "Found foo. Next starts at 9."
// 予想される結果: "Found foo. Next starts at 19."
}
```

Expand Down Expand Up @@ -63,6 +62,8 @@ JavaScript の {{jsxref("RegExp")}} オブジェクトは、 [global](/ja/docs/W
- グローバル正規表現のすべての出現を探す場合で、キャプチャグループのような情報が不要な場合は、代わりに {{jsxref("String.prototype.match()")}} を使用してください。さらに、 {{jsxref("String.prototype.matchAll()")}} は、一致した文字列を反復処理することで、(キャプチャグループを持つ)文字列の複数の部分の照合を簡略化するのに役立ちます。
- 文字列内の位置のインデックスを知るため照合する場合は、代わりに{{jsxref("String.prototype.search()")}}メソッドを使用してください。

`exec()`は、上記のいずれの方法でも容易に実現できない複雑な操作に有用です。特に、手動で [`lastIndex`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) を調整する必要がある場合に頻繁に使用されます。({{jsxref("String.prototype.matchAll()")}} は正規表現をコピーするため、`matchAll` の反復処理中に `lastIndex` を変更しても反復処理には影響しません。)その一例については、[`lastIndex` の巻き戻し](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex#lastindex_の巻き戻し) を参照してください。

## 例

### exec() の使用
Expand All @@ -87,7 +88,7 @@ const result = re.exec("The Quick Brown Fox Jumps Over The Lazy Dog");
| `index` | `4` |
| `indices` | `[[4, 25], [10, 15], [20, 25]]`<br />`groups: { color: [10, 15 ]}` |
| `input` | `"The Quick Brown Fox Jumps Over The Lazy Dog"` |
| `groups` | `{ color: "brown" }` |
| `groups` | `{ color: "Brown" }` |

それに加えて、この正規表現がグローバルであるため、 `re.lastIndex` は `25` に設定されます。

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
---
title: RegExp.prototype.global
short-title: global
slug: Web/JavaScript/Reference/Global_Objects/RegExp/global
l10n:
sourceCommit: 6fbdb78c1362fae31fbd545f4b2d9c51987a6bca
sourceCommit: cd22b9f18cf2450c0cc488379b8b780f0f343397
---

{{JSRef}}

**`global`** は {{jsxref("RegExp")}} インスタンスのプロパティで、`g` フラグが正規表現で使われているかどうかを返します。

{{InteractiveExample("JavaScript デモ: RegExp.prototype.global")}}

```js interactive-example
const regex1 = new RegExp("foo", "g");
const regex1 = /foo/g;

console.log(regex1.global);
// Expected output: true
// 予想される結果: true

const regex2 = new RegExp("bar", "i");
const regex2 = /bar/i;

console.log(regex2.global);
// Expected output: false
// 予想される結果: false
```

## 解説
Expand All @@ -36,16 +35,13 @@ console.log(regex2.global);
### global の使用

```js
const regex = /foo/g;
console.log(regex.global); // true
const globalRegex = /foo/g;

const str = "fooexamplefoo";
const str1 = str.replace(regex, "");
console.log(str1); // example
console.log(str.replace(globalRegex, "")); // example

const regex1 = /foo/;
const str2 = str.replace(regex1, "");
console.log(str2); // examplefoo
const nonGlobalRegex = /foo/;
console.log(str.replace(nonGlobalRegex, "")); // examplefoo
```

## 仕様書
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
---
title: "RegExp: lastIndex"
short-title: lastIndex
slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex
l10n:
sourceCommit: 846e3b1aa04ecceab70d379a9cc0717893707880
sourceCommit: cd22b9f18cf2450c0cc488379b8b780f0f343397
---

{{JSRef}}

**`lastIndex`** は {{jsxref("RegExp")}} インスタンスのデータプロパティで、次の照合を開始する位置を指定します。

{{InteractiveExample("JavaScript デモ: RegExp.lastIndex")}}

```js interactive-example
const regex1 = new RegExp("foo", "g");
const str1 = "table football, foosball";
const regex = /foo/g;
const str = "table football, foosball";

regex1.test(str1);
regex.test(str);

console.log(regex1.lastIndex);
// Expected output: 9
console.log(regex.lastIndex);
// 予想される結果: 9

regex1.test(str1);
regex.test(str);

console.log(regex1.lastIndex);
// Expected output: 19
console.log(regex.lastIndex);
// 予想される結果: 19
```

## 値
Expand All @@ -34,7 +33,7 @@ console.log(regex1.lastIndex);

## 解説

このプロパティは、正規表現インスタンスがグローバル検索を示すために `g` フラグを使用した場合、または粘着的検索を示すために `y` フラグを使用した場合にのみ設定されます。{{jsxref("RegExp.prototype.exec()", "exec()")}} が指定された入力に対して呼び出されたとき、以下の規則が適用されます。
このプロパティは、正規表現インスタンスがグローバル検索を示すために `g` フラグを使用した場合、または粘着的検索を示すために `y` フラグを使用した場合にのみ設定されます。{{jsxref("RegExp/exec", "exec()")}} が指定された入力に対して呼び出されたとき、以下の規則が適用されます。

- `lastIndex` が入力の長さよりも大きい場合、 `exec()` は一致するものを見つけられず、 `lastIndex` は 0 に設定されます。
- `lastIndex` が入力の長さ以下であった場合、 `exec()` は `lastIndex` から一致するものを探そうとします。
Expand Down Expand Up @@ -199,11 +198,11 @@ console.log(matchFoo("foo baz", 0)[0]); // "foo"

## 関連情報

- {{JSxRef("RegExp.prototype.dotAll")}}
- {{JSxRef("RegExp.prototype.global")}}
- {{JSxRef("RegExp.prototype.hasIndices")}}
- {{JSxRef("RegExp.prototype.ignoreCase")}}
- {{JSxRef("RegExp.prototype.multiline")}}
- {{JSxRef("RegExp.prototype.source")}}
- {{JSxRef("RegExp.prototype.sticky")}}
- {{JSxRef("RegExp.prototype.unicode")}}
- {{jsxref("RegExp.prototype.dotAll")}}
- {{jsxref("RegExp.prototype.global")}}
- {{jsxref("RegExp.prototype.hasIndices")}}
- {{jsxref("RegExp.prototype.ignoreCase")}}
- {{jsxref("RegExp.prototype.multiline")}}
- {{jsxref("RegExp.prototype.source")}}
- {{jsxref("RegExp.prototype.sticky")}}
- {{jsxref("RegExp.prototype.unicode")}}
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
---
title: RegExp.prototype.source
short-title: source
slug: Web/JavaScript/Reference/Global_Objects/RegExp/source
l10n:
sourceCommit: 16bacf2194dc9e9ff6ee5bcc65316547cf88a8d9
sourceCommit: cd22b9f18cf2450c0cc488379b8b780f0f343397
---

{{JSRef}}

**`source`** は {{jsxref("RegExp")}} のアクセサープロパティで、正規表現オブジェクトのソーステキストの入った文字列を返します。これには、両端の 2 つのスラッシュやフラグは含まれません。

{{InteractiveExample("JavaScript デモ: RegExp.prototype.source")}}

```js interactive-example
const regex1 = /fooBar/gi;
const regex = /fooBar/gi;

console.log(regex1.source);
// Expected output: "fooBar"
console.log(regex.source);
// 予想される結果: "fooBar"

console.log(new RegExp().source);
// Expected output: "(?:)"
// 予想される結果: "(?:)"

console.log(new RegExp("\n").source === "\\n");
// Expected output: true (starting with ES5)
// 予想される結果: true ES5 以降)
// Due to escaping
```

## 解説

概念的には、`source` プロパティは正規表現リテラルの 2 つのスラッシュの間のテキストです。この言語では、返される文字列が適切にエスケープされている必要があり、`source` の両端にスラッシュを連結すると、解析可能な正規表現リテラルが形成されます。例えば、`new RegExp("/")` の場合、`source` は `\\/` です。これは `/` を生成し、結果のリテラルは `///` です。同様に、[改行文字](/ja/docs/Web/JavaScript/Reference/Lexical_grammar#改行文字)はすべてエスケープされます。結果が解析可能である限り、他にも文字は必要ありません。空文字列の正規表現では `(?:)` という文字列を返します。
概念的には、`source` プロパティは正規表現リテラルの 2 つのスラッシュの間のテキストです。この言語では、返される文字列が適切にエスケープされている必要があり、`source` の両端にスラッシュを連結すると、解析可能な正規表現リテラルが形成されます。例えば、`new RegExp("/")` の場合、`source` は `\\/` です。これは `/` を生成し、結果のリテラルは `///` です。同様に、[改行文字](/ja/docs/Web/JavaScript/Reference/Lexical_grammar#改行文字)は、改行文字である文字が、正規表現リテラルを分割してしまうため、すべてエスケープされます。結果が解析可能である限り、他にも文字は必要ありません。空文字列の正規表現では `(?:)` という文字列を返します。

## 例

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
---
title: RegExp.prototype.sticky
short-title: sticky
slug: Web/JavaScript/Reference/Global_Objects/RegExp/sticky
l10n:
sourceCommit: 6fbdb78c1362fae31fbd545f4b2d9c51987a6bca
sourceCommit: cd22b9f18cf2450c0cc488379b8b780f0f343397
---

{{JSRef}}

**`sticky`** は {{jsxref("RegExp")}} インスタンスのアクセサープロパティで、この正規表現に `y` フラグが使用されているかどうかを返します。

{{InteractiveExample("JavaScript デモ: RegExp.prototype.sticky", "taller")}}

```js interactive-example
const str1 = "table football";
const regex1 = new RegExp("foo", "y");
const str = "table football";
const regex = /foo/y;

regex1.lastIndex = 6;
regex.lastIndex = 6;

console.log(regex1.sticky);
// Expected output: true
console.log(regex.sticky);
// 予想される結果: true

console.log(regex1.test(str1));
// Expected output: true
console.log(regex.test(str));
// 予想される結果: true

console.log(regex1.test(str1));
// Expected output: false
console.log(regex.test(str));
// 予想される結果: false
```

## 解説
Expand Down Expand Up @@ -87,9 +86,9 @@ Firefox の SpiderMonkey エンジンの一部のバージョンでは `^` 指
以下は正しい挙動の例です。

```js
const regex = /^foo/y;
regex.lastIndex = 2;
regex.test("..foo"); // false - インデックス 2 は文字列の先頭ではない
const regex1 = /^foo/y;
regex1.lastIndex = 2;
regex1.test("..foo"); // false - インデックス 2 は文字列の先頭ではない

const regex2 = /^foo/my;
regex2.lastIndex = 2;
Expand Down