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,11 +1,12 @@
---
title: RegExp.prototype.compile()
short-title: compile()
slug: Web/JavaScript/Reference/Global_Objects/RegExp/compile
l10n:
sourceCommit: 6bd17cb9cbc2d11163617b9f71706e93fdd743c8
sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166
---

{{JSRef}} {{deprecated_header}}
{{Deprecated_Header}}

> [!NOTE]
> `compile()` メソッドは互換性のためにのみ定義されています。`compile()` を使用すると、それまで不変であった正規表現のソースとフラグが変更可能なものとなり、ユーザーの期待を裏切る可能性があります。代わりに [`RegExp()`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) コンストラクターを使用して新しい正規表現オブジェクトを構築してください。
Expand All @@ -25,14 +26,18 @@ compile(pattern, flags)
- `flags`
- : [フラグ値](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#flags)の組み合わせです。

### 返値

なし ({{jsxref("undefined")}})。

##

### compile() の使用

次の例では、新しいパターンとフラグで正規表現を再コンパイルする方法を示します。

```js
const regexObj = new RegExp("foo", "gi");
const regexObj = /foo/gi;
regexObj.compile("new foo", "g");
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
---
title: RegExp.prototype.dotAll
short-title: dotAll
slug: Web/JavaScript/Reference/Global_Objects/RegExp/dotAll
l10n:
sourceCommit: 16bacf2194dc9e9ff6ee5bcc65316547cf88a8d9
sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166
---

{{JSRef}}

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

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

```js interactive-example
const regex1 = new RegExp("foo", "s");
const regex1 = /f.o/s;

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

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

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

## 解説
Expand All @@ -32,7 +31,28 @@ console.log(regex2.dotAll);
- U+2028 LINE SEPARATOR
- U+2029 PARAGRAPH SEPARATOR

これは事実上、ドットが基本多言語面 (BMP) のすべての文字と一致することを意味します。アストラル文字と一致させるには、`u` (Unicode) フラグを使用する必要があります。両方のフラグを組み合わせて使用すると、ドットは例外なく任意の Unicode 文字に一致します。
これは事実上、ドットが任意の UTF-16 コード単位に一致することを意味します。ただし、 Unicode 基本多言語面 (BMP) 外にある文字、いわゆるアストラル文字(アストラル文字は[サロゲートペア](/ja/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_文字、unicode_コードポイント、書記素クラスター)で表され、 1 つではなく 2 つの `.` パターンでの一致が必要となります。

```js
"😄".match(/(.)(.)/s);
// Array(3) [ "😄", "\ud83d", "\ude04" ]
```

[`u`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode) (unicode) フラグを使用すると、ドットがアストラル文字を単一文字として一致させることができます。

```js
"😄".match(/./su);
// Array [ "😄" ]
```

なお、`.*` のようなパターンは、`u` フラグがなくても、より大きなコンテキストの一部としてアストラル文字を消費する能力があります。

```js
"😄".match(/.*/s);
// Array [ "😄" ]
```

`s` フラグと `u` フラグを併用することで、ドットがより直感的な方法で任意の Unicode 文字に一致するようになります。

`dotAll` の設定アクセサーは `undefined` です。このプロパティを直接変更することはできません。

Expand Down Expand Up @@ -71,11 +91,11 @@ console.log(str2.replace(regex2, ""));
## 関連情報

- [`dotAll` フラグのポリフィル (`core-js`)](https://github.com/zloirock/core-js#ecmascript-string-and-regexp)
- {{JSxRef("RegExp.prototype.lastIndex")}}
- {{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.lastIndex")}}
- {{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,12 +1,11 @@
---
title: RegExp.escape()
short-title: escape()
slug: Web/JavaScript/Reference/Global_Objects/RegExp/escape
l10n:
sourceCommit: a73295d4344aeab38c67262717d0dda8b3b9f0c5
sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166
---

{{JSRef}}

**`RegExp.escape()`** は静的メソッドで、文字列内の潜在的な正規表現構文文字を[エスケープ](/ja/docs/Web/JavaScript/Reference/Regular_expressions#エスケープシーケンス)し、[リテラルパターン](/ja/docs/Web/JavaScript/Reference/Regular_expressions/Literal_character)として {{jsxref("RegExp/RegExp", "RegExp()")}} コンストラクターで安全に使用できる新しい文字列を返します。

ユーザーが提供したコンテンツを含む {{jsxref("RegExp")}} を動的に作成する場合は、この関数を使用して入力を無害化することを検討してください(入力が実際に正規表現構文を含むように意図されている場合を除く)。また、例えば、 {{jsxref("String.prototype.replaceAll()")}} を使用して、すべての構文文字の前に `\` を挿入するなどして、その機能を再実装しようとしないでください。 `RegExp.escape()` は、手作業で作成したコードではおそらく達成できないであろう、複数のエッジケース/コンテキストで動作するエスケープシーケンスを使用するように設計されています。
Expand Down Expand Up @@ -105,4 +104,5 @@ function removeDomain(text, domain) {
## 関連情報

- [`RegExp.escape` のポリフィル (`core-js`)](https://github.com/zloirock/core-js#regexp-escaping)
- [es-shims による `Reflect.escape` のポリフィル](https://www.npmjs.com/package/regexp.escape)
- {{jsxref("RegExp")}}
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
---
title: RegExp.prototype.flags
short-title: flags
slug: Web/JavaScript/Reference/Global_Objects/RegExp/flags
l10n:
sourceCommit: fc67640f3545c1a5db42c878d1f0de71313349bc
sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166
---

{{JSRef}}

**`flags`** は {{jsxref("RegExp")}} インスタンスのプロパティで、現在の正規表現オブジェクトの[フラグ](/ja/docs/Web/JavaScript/Guide/Regular_expressions#フラグを用いた高度な検索)から成る文字列を返します。

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

```js interactive-example
// Outputs RegExp flags in alphabetical order
// 正規表現フラグをアルファベット順に出力

console.log(/foo/gi.flags);
// Expected output: "gi"
// 予想される結果: "gi"

console.log(/bar/muy.flags);
// Expected output: "muy"
console.log(/^bar/muy.flags);
// 予想される結果: "muy"
```

## 解説
Expand All @@ -35,7 +34,7 @@ console.log(/bar/muy.flags);

```js-nolint
/foo/ig.flags; // "gi"
/bar/myu.flags; // "muy"
/^bar/myu.flags; // "muy"
```

## 仕様書
Expand All @@ -49,4 +48,6 @@ console.log(/bar/muy.flags);
## 関連情報

- [`RegExp.prototype.flags` のポリフィル (`core-js`)](https://github.com/zloirock/core-js#ecmascript-string-and-regexp)
- {{JSxRef("RegExp.prototype.source")}}
- [es-shims による `RegExp.prototype.flags` のポリフィル](https://www.npmjs.com/package/regexp.prototype.flags)
- [フラグを用いた高度な検索](/ja/docs/Web/JavaScript/Guide/Regular_expressions#フラグを用いた高度な検索)(正規表現ガイド)
- {{jsxref("RegExp.prototype.source")}}
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
---
title: RegExp.prototype.hasIndices
short-title: hasIndices
slug: Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices
l10n:
sourceCommit: 16bacf2194dc9e9ff6ee5bcc65316547cf88a8d9
sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166
---

{{JSRef}}

**`hasIndices`** は {{jsxref("RegExp")}} インスタンスのプロパティで、その正規表現で `d` フラグが使用されたかどうかを示します。

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

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

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

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

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

## 解説
Expand Down Expand Up @@ -66,12 +65,12 @@ console.log(regex2.exec(str2).indices); // undefined

## 関連情報

- {{JSxRef("RegExp.prototype.lastIndex")}}
- {{JSxRef("RegExp.prototype.exec()")}}
- {{JSxRef("RegExp.prototype.dotAll")}}
- {{JSxRef("RegExp.prototype.global")}}
- {{JSxRef("RegExp.prototype.ignoreCase")}}
- {{JSxRef("RegExp.prototype.multiline")}}
- {{JSxRef("RegExp.prototype.source")}}
- {{JSxRef("RegExp.prototype.sticky")}}
- {{JSxRef("RegExp.prototype.unicode")}}
- {{jsxref("RegExp.prototype.lastIndex")}}
- {{jsxref("RegExp.prototype.exec()")}}
- {{jsxref("RegExp.prototype.dotAll")}}
- {{jsxref("RegExp.prototype.global")}}
- {{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,28 +1,27 @@
---
title: RegExp.prototype.ignoreCase
short-title: ignoreCase
slug: Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase
l10n:
sourceCommit: 16bacf2194dc9e9ff6ee5bcc65316547cf88a8d9
sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166
---

{{JSRef}}

**`ignoreCase`** プロパティは、"`i`" フラグが正規表現で使われているかどうかを示します。`ignoreCase` は、正規表現インスタンスごとの読み取り専用プロパティです。
**`ignoreCase`** は {{jsxref("RegExp")}} インスタンスのアクセサープロパティで、`i` フラグが正規表現で使われているかどうかを示します。

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

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

console.log(regex1.test("Football"));
// Expected output: false
// 予想される結果: false

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

console.log(regex2.test("Football"));
// Expected output: true
// 予想される結果: true
```

## 解説
Expand All @@ -31,7 +30,9 @@ console.log(regex2.test("Football"));

正規表現が [Unicode 対応](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode#unicode-aware_mode)の場合、大文字小文字の対応付けは [`CaseFolding.txt`](https://unicode.org/Public/UCD/latest/ucd/CaseFolding.txt) で指定された*単純な大文字小文字の変換*を行います。この対応付けは常に 1 つのコードポイントに対応付けされるので、例えば `ß` (U+00DF LATIN SMALL LETTER SHARP S) を `ss` に対応付けすることはありません(単純な大文字小文字の変換ではなく完全な大文字小文字の変換です)。例えば、`ſ` (U+017F LATIN SMALL LETTER LONG S) は `s` (U+0073 LATIN SMALL LETTER S) に、`K` (U+212A KELVIN SIGN) は `k` (U+006B LATIN SMALL LETTER K) に大文字小文字を区別します。したがって、`ſ` と `K` は `/[a-z]/ui` で一致します。

正規表現が Unicode 非対応の場合、大文字小文字の対応付けは [Unicode 既定の大文字小文字変換](https://unicode-org.github.io/icu/userguide/transforms/casemappings.html) を使用します。{{jsxref("String.prototype.toUpperCase()")}} で使用されているアルゴリズムと同じです。例えば、`Ω` (U+2126 OHM SIGN, オーム記号) と `Ω` (U+03A9 GREEK CAPITAL LETTER OMEGA, ギリシャ語のオメガの大文字) は、既定の大文字小文字変換によってそれ自体に対応付けされますが、単純な大文字小文字の変換では `ω` (U+03C9 GREEK SMALL LETTER OMEGA, ギリシャ語のオメガの小文字) に対応付けされるため、`"ω"` は `/[\u2126]/ui` と `/[\u03a9]/ui` に一致しますが、`/[\u2126]/i` や `/[\u03a9]/i` には一致しません。このアルゴリズムは、基本ラテンブロック外のコードポイントがブロック内のコードポイントに対応付けされるのを防ぐため、前述の `ſ` や `K` は `/[a-z]/i` では一致しません。
正規表現が Unicode 非対応の場合、大文字小文字の対応付けは [Unicode 既定の大文字小文字変換](https://unicode-org.github.io/icu/userguide/transforms/casemappings.html) を使用します。{{jsxref("String.prototype.toUpperCase()")}} で使用されているアルゴリズムと同じです。このアルゴリズムは、基本ラテン文字ブロック外のコードポイントが同ブロック内のコードポイントにマッピングされるのを防ぐため、前述の `ſ` や `K` は `/[a-z]/i` では一致しません。

Unicode 対応の大文字小文字変換は、通常小文字に変換される一方、Unicode 非対応の大文字小文字変換は大文字に変換されます。これら二者は完全な逆操作ではないため、微妙な動作の違いが存在します。例えば、`Ω` (U+2126 OHM SIGN, オーム記号) と `Ω` (U+03A9 GREEK CAPITAL LETTER OMEGA, ギリシャ語のオメガの大文字) は、既定の大文字小文字変換によってそれ自体に対応付けされますが、単純な大文字小文字の変換では `ω` (U+03C9 GREEK SMALL LETTER OMEGA, ギリシャ語のオメガの小文字) に対応付けされるため、`"\u2126"` は `/[\u03c9]/ui` と `/[\u03a9]/ui` に一致します。一方、U+2126 はデフォルトの大文字小文字変換によって自身にマッピングさ れますが、他の二つは両方とも U+03A9 にマッピングされるため、`"\u2126"` は `/[\u03c9]/i` にも `/[\u03a9]/i` にも一致しません。

`ignoreCase` の設定アクセサーは `undefined` です。このプロパティを直接変更することはできません。

Expand All @@ -56,10 +57,10 @@ console.log(regex.ignoreCase); // true
## 関連情報

- {{jsxref("RegExp.prototype.lastIndex")}}
- {{JSxRef("RegExp.prototype.dotAll")}}
- {{JSxRef("RegExp.prototype.global")}}
- {{JSxRef("RegExp.prototype.hasIndices")}}
- {{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.multiline")}}
- {{jsxref("RegExp.prototype.source")}}
- {{jsxref("RegExp.prototype.sticky")}}
- {{jsxref("RegExp.prototype.unicode")}}
Loading