diff --git a/files/ja/web/javascript/reference/global_objects/regexp/compile/index.md b/files/ja/web/javascript/reference/global_objects/regexp/compile/index.md index 93aff218e1cdf4..2c98698586b4bd 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/compile/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/compile/index.md @@ -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) コンストラクターを使用して新しい正規表現オブジェクトを構築してください。 @@ -25,6 +26,10 @@ compile(pattern, flags) - `flags` - : [フラグ値](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#flags)の組み合わせです。 +### 返値 + +なし ({{jsxref("undefined")}})。 + ## 例 ### compile() の使用 @@ -32,7 +37,7 @@ compile(pattern, flags) 次の例では、新しいパターンとフラグで正規表現を再コンパイルする方法を示します。 ```js -const regexObj = new RegExp("foo", "gi"); +const regexObj = /foo/gi; regexObj.compile("new foo", "g"); ``` diff --git a/files/ja/web/javascript/reference/global_objects/regexp/dotall/index.md b/files/ja/web/javascript/reference/global_objects/regexp/dotall/index.md index 31dbe5e888e600..00cc2f979ca4d8 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/dotall/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/dotall/index.md @@ -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 ``` ## 解説 @@ -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` です。このプロパティを直接変更することはできません。 @@ -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")}} diff --git a/files/ja/web/javascript/reference/global_objects/regexp/escape/index.md b/files/ja/web/javascript/reference/global_objects/regexp/escape/index.md index 3c5a3d53434a84..1196bd06cf9b4a 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/escape/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/escape/index.md @@ -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()` は、手作業で作成したコードではおそらく達成できないであろう、複数のエッジケース/コンテキストで動作するエスケープシーケンスを使用するように設計されています。 @@ -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")}} diff --git a/files/ja/web/javascript/reference/global_objects/regexp/flags/index.md b/files/ja/web/javascript/reference/global_objects/regexp/flags/index.md index 6db4709cdeeb84..e4a941981e9ab7 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/flags/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/flags/index.md @@ -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" ``` ## 解説 @@ -35,7 +34,7 @@ console.log(/bar/muy.flags); ```js-nolint /foo/ig.flags; // "gi" -/bar/myu.flags; // "muy" +/^bar/myu.flags; // "muy" ``` ## 仕様書 @@ -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")}} diff --git a/files/ja/web/javascript/reference/global_objects/regexp/hasindices/index.md b/files/ja/web/javascript/reference/global_objects/regexp/hasindices/index.md index 7b577eb27d426d..ec3f06d17cabe3 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/hasindices/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/hasindices/index.md @@ -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 ``` ## 解説 @@ -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")}} diff --git a/files/ja/web/javascript/reference/global_objects/regexp/ignorecase/index.md b/files/ja/web/javascript/reference/global_objects/regexp/ignorecase/index.md index 8155d0f236c896..d337ed65b54cc2 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/ignorecase/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/ignorecase/index.md @@ -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 ``` ## 解説 @@ -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` です。このプロパティを直接変更することはできません。 @@ -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")}} diff --git a/files/ja/web/javascript/reference/global_objects/regexp/index.md b/files/ja/web/javascript/reference/global_objects/regexp/index.md index a40c500526d438..8fa45ca163f06a 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/index.md @@ -2,11 +2,9 @@ title: RegExp slug: Web/JavaScript/Reference/Global_Objects/RegExp l10n: - sourceCommit: a73295d4344aeab38c67262717d0dda8b3b9f0c5 + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`RegExp`** オブジェクトは、テキストをパターンと照合するために使用します。 正規表現の入門記事は、 JavaScript ガイドの[正規表現](/ja/docs/Web/JavaScript/Guide/Regular_expressions)をお読みください。正規表現の構文の詳細情報については、[正規表現リファレンス](/ja/docs/Web/JavaScript/Reference/Regular_expressions)を参照してください。 @@ -15,10 +13,10 @@ l10n: ### リテラル記法とコンストラクター -`RegExp` オブジェクトを生成するには 2 通りの方法があります。*リテラル記法*と*コンストラクター*です。 +`RegExp` オブジェクトを生成するには 2 通りの方法があります。「リテラル記法」と「コンストラクター」です。 -- _リテラル記法_ はパターンを 2 本のスラッシュで囲み、 2 本目のスラッシュの後にオプションで[フラグ](/ja/docs/Web/JavaScript/Guide/Regular_expressions#フラグを用いた高度な検索)が続きます。 -- _コンストラクター関数_ は文字列または `RegExp` オブジェクトを最初の引数として取り、オプションの[フラグ](/ja/docs/Web/JavaScript/Guide/Regular_expressions#フラグを用いた高度な検索)を文字列で 2 番目の引数として取ります。 +- 「リテラル記法」はパターンを 2 本のスラッシュで囲み、 2 本目のスラッシュの後にオプションで[フラグ](/ja/docs/Web/JavaScript/Guide/Regular_expressions#フラグを用いた高度な検索)が続きます。 +- 「コンストラクター関数」は文字列または `RegExp` オブジェクトを最初の引数として取り、オプションの[フラグ](/ja/docs/Web/JavaScript/Guide/Regular_expressions#フラグを用いた高度な検索)を文字列で 2 番目の引数として取ります。 以下の 3 つの式は、同じ正規表現オブジェクトを生成します。 @@ -30,7 +28,7 @@ const re = new RegExp("ab+c", "i"); // 最初の引数に文字列のパター const re = new RegExp(/ab+c/, "i"); // 最初の引数に正規表現リテラルを渡したコンストラクター ``` -正規表現は使用できるようになる前に、コンパイルする必要があります。この処理によって、より効率的に一致を行うことができるようになります。この処理の詳細は[ドットネットのドキュメント](https://learn.microsoft.com/dotnet/standard/base-types/compilation-and-reuse-in-regular-expressions)に記載されています。 +正規表現は使用できるようになる前に、コンパイルする必要があります。この処理によって、より効率的に照合を行うことができるようになります。この処理の詳細は[ドットネットのドキュメント](https://learn.microsoft.com/ja-jp/dotnet/standard/base-types/compilation-and-reuse-in-regular-expressions)に記載されています。 リテラル記法では、正規表現が評価されるときにコンパイルが行われます。一方、`RegExp` オブジェクトのコンストラクターである `new RegExp('ab+c')` は、実行時に正規表現をコンパイルすることになります。 @@ -40,7 +38,7 @@ const re = new RegExp(/ab+c/, "i"); // 最初の引数に正規表現リテラ `new RegExp(/ab+c/, flags)` という式は新しい `RegExp` を生成しますが、第 1 引数を元として使用し、第 2 引数で指定された[フラグ](/ja/docs/Web/JavaScript/Guide/Regular_expressions#フラグを用いた高度な検索)として使用して生成します。 -コンストラクター関数を使用する場合は、通常の文字エスケープ規則(文字列内に特殊文字が含まれるとき、前に `\` を付加する)が必須です。 +コンストラクター関数を使用する場合は、通常の文字エスケープの規則(文字列内に特殊文字が含まれるとき、前に `\` を付加する)が必須です。 例えば、以下 2 つの構文は同等です。 @@ -177,15 +175,15 @@ re.exec("bar"); // [ 'bar', index: 0, input: 'bar', groups: undefined ] ### 正規表現を使用したデータ形式の変更 -以下のスクリプトは、 {{jsxref("String.prototype.replace()")}} メソッドを使用して、 _名 姓_ の形式の名前に一致させ、_姓, 名_ の形式で出力します。 +以下のスクリプトは、 {{jsxref("String.prototype.replace()")}} メソッドを使用して、 _名 姓_ の形式の名前で照合し、_姓, 名_ の形式で出力します。 スクリプトでは、置換テキスト中で `$1` と `$2` を使用して、それぞれ対応する正規表現パターンで一致する括弧がキャプチャした結果を指定しています。 ```js const re = /(\w+)\s(\w+)/; const str = "Maria Cruz"; -const newstr = str.replace(re, "$2, $1"); -console.log(newstr); +const newStr = str.replace(re, "$2, $1"); +console.log(newStr); ``` これは、 `"Cruz, Maria"` と表示します。 @@ -195,8 +193,8 @@ console.log(newstr); 既定の改行文字は、プラットフォーム (Unix、Windows など) によって異なります。この例で実行する行分割は、あらゆるプラットフォームで動作します。 ```js -const text = "Some text\nAnd some more\r\nAnd yet\rThis is the end"; -const lines = text.split(/\r\n|\r|\n/); +const text = "Some text\nAnd some more\r\nAnd yet\nThis is the end"; +const lines = text.split(/\r?\n/); console.log(lines); // [ 'Some text', 'And some more', 'And yet', 'This is the end' ] ``` @@ -204,13 +202,15 @@ console.log(lines); // [ 'Some text', 'And some more', 'And yet', 'This is the e ### 複数行で正規表現を使用する +デフォルトでは、`.` 文字は改行に一致しません。改行に一致させるには、`s` フラグ(`dotAll` モード)を使用します。 + ```js const s = "Please yes\nmake my day!"; s.match(/yes.*day/); // null を返す -s.match(/yes[^]*day/); +s.match(/yes.*day/s); // Returns ["yes\nmake my day"] ``` @@ -252,13 +252,13 @@ global フラグ `g` を付けると、 3 桁だけでなく、 6 桁すべて 上の表にもある通り、`\w` や `\W` は ASCII 基本文字にのみ一致します。具体的には `a` から `z` 、`A` から `Z` 、 `0` から `9` および `_` です。 -キリル語やヘブライ語で使われるような非 ASCII 文字に一致させるには `\uhhhh` 形式 (`hhhh` の部分は 16 進表記の Unicode 値) を使ってください。 +キリル語やヘブライ語で使われるような非 ASCII 文字と照合するには `\uHHHH` 形式(`HHHH` の部分は 16 進表記の Unicode 値)を使ってください。 この例は、文字列全体から Unicode 文字列だけを抜き出す方法を示しています。 ```js const text = "Образец text на русском языке"; -const regex = /[\u0400-\u04FF]+/g; +const regex = /[\u0400-\u04ff]+/g; const match = regex.exec(text); console.log(match[0]); // 'Образец' diff --git a/files/ja/web/javascript/reference/global_objects/regexp/input/index.md b/files/ja/web/javascript/reference/global_objects/regexp/input/index.md index 9d17d89dcd740b..4a33fbf1dab856 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/input/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/input/index.md @@ -1,11 +1,12 @@ --- title: RegExp.input ($_) +short-title: input ($_) slug: Web/JavaScript/Reference/Global_Objects/RegExp/input l10n: - sourceCommit: fb85334ffa4a2c88d209b1074909bee0e0abd57a + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} {{Deprecated_Header}} +{{Deprecated_Header}} > [!NOTE] > グローバルに最後の一致状態を公開する `RegExp` の静的プロパティは、すべて非推奨です。詳しくは[非推奨の RegExp 機能](/ja/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#regexp)を参照してください。 @@ -14,7 +15,7 @@ l10n: ## 解説 -`input` は {{jsxref("RegExp")}} の静的静的プロパティですので、作成した `RegExp` オブジェクト野プロパティとしてではなく、常に `RegExp.input` または `RegExp.$_` として使用してください。 +`input` は {{jsxref("RegExp")}} の静的プロパティですので、作成した `RegExp` オブジェクト野プロパティとしてではなく、常に `RegExp.input` または `RegExp.$_` として使用してください。 `input` の値は `RegExp` インスタンス(ただし `RegExp` のサブクラスではない)が照合に成功するたびに更新されます。今まで一度も一致するものがなかった場合、 `input` は空文字列です。`input` に値を設定することはできますが、正規表現の他の動作には影響せず、値は次に行われた照合が成功した場合に再び上書きされます。 @@ -42,8 +43,8 @@ RegExp.$_; // "hi world!" ## 関連情報 -- {{jsxref("RegExp/lastMatch", "RegExp.lastMatch ($&)")}} -- {{jsxref("RegExp/lastParen", "RegExp.lastParen ($+)")}} -- {{jsxref("RegExp/leftContext", "RegExp.leftContext ($`)")}} -- {{jsxref("RegExp/rightContext", "RegExp.rightContext ($')")}} -- {{jsxref("RegExp/n", "RegExp.$1, …, RegExp.$9")}} +- [`RegExp.lastMatch` (`$&`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch) +- [`RegExp.lastParen` (`$+`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastParen) +- [`RegExp.leftContext` (`` $` ``)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/leftContext) +- [`RegExp.rightContext` (`$'`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/rightContext) +- [`RegExp.$1`, …, `RegExp.$9`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n) diff --git a/files/ja/web/javascript/reference/global_objects/regexp/lastmatch/index.md b/files/ja/web/javascript/reference/global_objects/regexp/lastmatch/index.md index 0e9c8e13130940..10cb8278079136 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/lastmatch/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/lastmatch/index.md @@ -1,11 +1,12 @@ --- title: RegExp.lastMatch ($&) +short-title: lastMatch ($&) slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch l10n: - sourceCommit: d1edcbabf7431e9929c77e70b0c1bc741d887236 + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} {{deprecated_header}} +{{Deprecated_Header}} > [!NOTE] > グローバルに最後の一致状態を公開する `RegExp` の静的プロパティは、すべて非推奨です。詳しくは[非推奨の RegExp 機能](/ja/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#regexp)を参照してください。 @@ -43,8 +44,8 @@ RegExp["$&"]; // "hi" ## 関連情報 -- {{jsxref("RegExp.input", "RegExp.input ($_)")}} -- {{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}} -- {{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}} -- {{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}} -- {{jsxref("RegExp/n", "RegExp.$1, …, RegExp.$9")}} +- [`RegExp.input` (`$_`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/input) +- [`RegExp.lastParen` (`$+`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastParen) +- [`RegExp.leftContext` (`` $` ``)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/leftContext) +- [`RegExp.rightContext` (`$'`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/rightContext) +- [`RegExp.$1`, …, `RegExp.$9`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n) diff --git a/files/ja/web/javascript/reference/global_objects/regexp/lastparen/index.md b/files/ja/web/javascript/reference/global_objects/regexp/lastparen/index.md index 6f41835abe31b5..d307c0d24d60f1 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/lastparen/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/lastparen/index.md @@ -1,11 +1,12 @@ --- title: RegExp.lastParen ($+) +short-title: lastParen ($+) slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastParen l10n: - sourceCommit: d1edcbabf7431e9929c77e70b0c1bc741d887236 + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} {{deprecated_header}} +{{Deprecated_Header}} > [!NOTE] > グローバルに最後の一致状態を公開する `RegExp` の静的プロパティは、すべて非推奨です。詳しくは[非推奨の RegExp 機能](/ja/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#regexp)を参照してください。 @@ -41,8 +42,8 @@ RegExp["$+"]; // "hi" ## 関連情報 -- {{jsxref("RegExp.input", "RegExp.input ($_)")}} -- {{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}} -- {{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}} -- {{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}} -- {{jsxref("RegExp/n", "RegExp.$1, …, RegExp.$9")}} +- [`RegExp.input` (`$_`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/input) +- [`RegExp.lastMatch` (`$&`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch) +- [`RegExp.leftContext` (`` $` ``)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/leftContext) +- [`RegExp.rightContext` (`$'`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/rightContext) +- [`RegExp.$1`, …, `RegExp.$9`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n) diff --git a/files/ja/web/javascript/reference/global_objects/regexp/leftcontext/index.md b/files/ja/web/javascript/reference/global_objects/regexp/leftcontext/index.md index c0753838cd07ad..cd02812df6fa52 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/leftcontext/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/leftcontext/index.md @@ -1,11 +1,12 @@ --- title: RegExp.leftContext ($`) +short-title: leftContext ($`) slug: Web/JavaScript/Reference/Global_Objects/RegExp/leftContext l10n: - sourceCommit: 05218bd05ab482d49ca659473851a285bcb104b0 + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} {{deprecated_header}} +{{Deprecated_Header}} > [!NOTE] > グローバルに最後の一致状態を公開する `RegExp` の静的プロパティは、すべて非推奨です。詳しくは[非推奨の RegExp 機能](/ja/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#regexp)を参照してください。 @@ -20,7 +21,7 @@ l10n: ドットプロパティアクセサー (``RegExp.$` ``) で短縮エイリアスを使用することはできません。`` ` `` は識別子の一部として有効ではないので、{{jsxref("SyntaxError")}} が発生します。代わりに[ブラケット記法](/ja/docs/Web/JavaScript/Reference/Operators/Property_accessors)を使用してください。 -``$` `` は {{jsxref("String.prototype.replace()")}} の置換文字列でも使用できますが、``RegExp["$`"]`` の古いプロパティとは無関係です。 +`` $` `` は {{jsxref("String.prototype.replace()")}} の置換文字列でも使用できますが、``RegExp["$`"]`` の古いプロパティとは無関係です。 ## 例 @@ -43,8 +44,8 @@ RegExp["$`"]; // "hello " ## 関連情報 -- {{jsxref("RegExp.input", "RegExp.input ($_)")}} -- {{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}} -- {{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}} -- {{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}} -- {{jsxref("RegExp/n", "RegExp.$1, …, RegExp.$9")}} +- [`RegExp.input` (`$_`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/input) +- [`RegExp.lastMatch` (`$&`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch) +- [`RegExp.lastParen` (`$+`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastParen) +- [`RegExp.rightContext` (`$'`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/rightContext) +- [`RegExp.$1`, …, `RegExp.$9`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n) diff --git a/files/ja/web/javascript/reference/global_objects/regexp/multiline/index.md b/files/ja/web/javascript/reference/global_objects/regexp/multiline/index.md index 279b238de052ed..696c303af53aee 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/multiline/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/multiline/index.md @@ -1,31 +1,30 @@ --- title: RegExp.prototype.multiline +short-title: multiline slug: Web/JavaScript/Reference/Global_Objects/RegExp/multiline l10n: - sourceCommit: 16bacf2194dc9e9ff6ee5bcc65316547cf88a8d9 + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`multiline`** は {{jsxref("RegExp")}} のアクセサープロパティで、正規表現で `m` フラグが使用されているかどうかを返します。 {{InteractiveExample("JavaScript デモ: RegExp.prototype.multiline", "taller")}} ```js interactive-example -const regex1 = new RegExp("^football"); -const regex2 = new RegExp("^football", "m"); +const regex1 = /^football/; +const regex2 = /^football/m; console.log(regex1.multiline); -// Expected output: false +// 予想される結果: false console.log(regex2.multiline); -// Expected output: true +// 予想される結果: true console.log(regex1.test("rugby\nfootball")); -// Expected output: false +// 予想される結果: false console.log(regex2.test("rugby\nfootball")); -// Expected output: true +// 予想される結果: true ``` ## 解説 @@ -39,7 +38,7 @@ console.log(regex2.test("rugby\nfootball")); ### multiline の使用 ```js -const regex = /foo/m; +const regex = /^foo/m; console.log(regex.multiline); // true ``` @@ -55,10 +54,10 @@ console.log(regex.multiline); // true ## 関連情報 - {{jsxref("RegExp.prototype.lastIndex")}} -- {{JSxRef("RegExp.prototype.dotAll")}} -- {{JSxRef("RegExp.prototype.global")}} -- {{JSxRef("RegExp.prototype.hasIndices")}} -- {{JSxRef("RegExp.prototype.ignoreCase")}} -- {{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.source")}} +- {{jsxref("RegExp.prototype.sticky")}} +- {{jsxref("RegExp.prototype.unicode")}} diff --git a/files/ja/web/javascript/reference/global_objects/regexp/n/index.md b/files/ja/web/javascript/reference/global_objects/regexp/n/index.md index 128fe763a031f4..f952bce6a5d6d5 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/n/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/n/index.md @@ -1,11 +1,12 @@ --- title: RegExp.$1, …, RegExp.$9 +short-title: $1, …, $9 slug: Web/JavaScript/Reference/Global_Objects/RegExp/n l10n: - sourceCommit: f3df52530f974e26dd3b14f9e8d42061826dea20 + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} {{deprecated_header}} +{{Deprecated_Header}} > [!NOTE] > グローバルに最後の一致状態を公開する `RegExp` の静的プロパティは、すべて非推奨です。詳しくは[非推奨の RegExp 機能](/ja/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#regexp)を参照してください。 @@ -26,7 +27,7 @@ l10n: ### $n と RegExp.prototype.test() の使用 -次のスクリプトは、{{jsxref("RegExp")}} インスタンスの {{jsxref("RegExp.prototype.test()", "test()")}} メソッドを使用して、一般的な文字列で数値を取得しています。 +次のスクリプトは、{{jsxref("RegExp.prototype.test()")}} メソッドを使用して、一般的な文字列で数値を取得しています。 ```js const str = "Test 24"; @@ -46,8 +47,8 @@ number; // "24" ## 関連情報 -- {{jsxref("RegExp.input", "RegExp.input ($_)")}} -- {{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}} -- {{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}} -- {{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}} -- {{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}} +- [`RegExp.input` (`$_`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/input) +- [`RegExp.lastMatch` (`$&`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch) +- [`RegExp.lastParen` (`$+`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastParen) +- [`RegExp.leftContext` (`` $` ``)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/leftContext) +- [`RegExp.rightContext` (`$'`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/rightContext) diff --git a/files/ja/web/javascript/reference/global_objects/regexp/regexp/index.md b/files/ja/web/javascript/reference/global_objects/regexp/regexp/index.md index 892a419d30edb8..0b863a01ad4c0e 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/regexp/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/regexp/index.md @@ -1,30 +1,29 @@ --- title: RegExp() コンストラクター +short-title: RegExp() slug: Web/JavaScript/Reference/Global_Objects/RegExp/RegExp l10n: - sourceCommit: fc67640f3545c1a5db42c878d1f0de71313349bc + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`RegExp()`** コンストラクターは {{jsxref("RegExp")}} オブジェクトを生成します。 -正規表現について詳しく知りたい方は [JavaScript ガイド](/ja/docs/Web/JavaScript/Guide) の [正規表現](/ja/docs/Web/JavaScript/Guide/Regular_expressions) を参照してください。 +正規表現について詳しく知りたい方は [JavaScript ガイド](/ja/docs/Web/JavaScript/Guide)の[正規表現](/ja/docs/Web/JavaScript/Guide/Regular_expressions)を参照してください。 -{{InteractiveExample("JavaScript デモ: RegExp Constructor")}} +{{InteractiveExample("JavaScript デモ: RegExp() コンストラクター")}} ```js interactive-example const regex1 = /\w+/; const regex2 = new RegExp("\\w+"); console.log(regex1); -// Expected output: /\w+/ +// 予想される結果: /\w+/ console.log(regex2); -// Expected output: /\w+/ +// 予想される結果: /\w+/ console.log(regex1 === regex2); -// Expected output: false +// 予想される結果: false ``` ## 構文 @@ -79,7 +78,7 @@ RegExp(pattern, flags) ### 例外 - {{jsxref("SyntaxError")}} - - : 以下のいずれかが成立すると発生します。 + - : 以下のいずれかの場合に発生します。 - `pattern` が有効な正規表現として解釈できない場合 - `flags` に繰り返して使われた文字や、許可されている文字以外が含まれていた場合。 @@ -87,7 +86,7 @@ RegExp(pattern, flags) ### リテラル記法とコンストラクター -`RegExp` オブジェクトを生成するのに*リテラル記法*と*コンストラクター*の 2 つの方法があります。 +`RegExp` オブジェクトを生成するのに「リテラル記法」と「コンストラクター」の 2 つの方法があります。 - **リテラル記法**はパターンを 2 本のスラッシュの間に取り、2 番目のスラッシュの後にオプションのフラグが続くものです。 - **コンストラクター関数**は文字列または `RegExp` オブジェクトを最初の引数として取り、2 番目の引数としてオプションのフラグの文字列を取ります。 @@ -127,6 +126,6 @@ order.match(new RegExp(`\\b(${breakfasts.join("|")})\\b`, "g")); ## 関連情報 - [多くの現代的な `RegExp` 機能(`dotAll`、`sticky` フラグ、名前付きキャプチャグループなど)のポリフィル (`core-js`)](https://github.com/zloirock/core-js#ecmascript-string-and-regexp) -- [JavaScript ガイド](/ja/docs/Web/JavaScript/Guide)内の[正規表現](/ja/docs/Web/JavaScript/Guide/Regular_expressions)の章 +- [正規表現](/ja/docs/Web/JavaScript/Guide/Regular_expressions)ガイド - {{jsxref("String.prototype.match()")}} - {{jsxref("String.prototype.replace()")}} diff --git a/files/ja/web/javascript/reference/global_objects/regexp/rightcontext/index.md b/files/ja/web/javascript/reference/global_objects/regexp/rightcontext/index.md index bf541ec7c2292c..fc4977583dbdb9 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/rightcontext/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/rightcontext/index.md @@ -1,11 +1,12 @@ --- title: RegExp.rightContext ($') +short-title: rightContext ($') slug: Web/JavaScript/Reference/Global_Objects/RegExp/rightContext l10n: - sourceCommit: d1edcbabf7431e9929c77e70b0c1bc741d887236 + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} {{deprecated_header}} +{{Deprecated_Header}} > [!NOTE] > グローバルに最後の一致状態を公開する `RegExp` の静的プロパティは、すべて非推奨です。詳しくは[非推奨の RegExp 機能](/ja/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#regexp)を参照してください。 @@ -43,8 +44,8 @@ RegExp["$'"]; // " world!" ## 関連情報 -- {{jsxref("RegExp.input", "RegExp.input ($_)")}} -- {{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}} -- {{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}} -- {{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}} -- {{jsxref("RegExp/n", "RegExp.$1, …, RegExp.$9")}} +- [`RegExp.input` (`$_`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/input) +- [`RegExp.lastMatch` (`$&`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch) +- [`RegExp.lastParen` (`$+`)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastParen) +- [`RegExp.leftContext` (`` $` ``)](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/leftContext) +- [`RegExp.$1`, …, `RegExp.$9`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n) diff --git a/files/ja/web/javascript/reference/global_objects/regexp/symbol.match/index.md b/files/ja/web/javascript/reference/global_objects/regexp/symbol.match/index.md index 7d2baca1f251f8..ad4b614cdc888a 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/symbol.match/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/symbol.match/index.md @@ -1,12 +1,11 @@ --- title: RegExp.prototype[Symbol.match]() +short-title: "[Symbol.match]()" slug: Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.match l10n: - sourceCommit: 6fbdb78c1362fae31fbd545f4b2d9c51987a6bca + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`[Symbol.match]()`** は {{jsxref("RegExp")}} インスタンスのメソッドで、 [`String.prototype.match()`](/ja/docs/Web/JavaScript/Reference/Global_Objects/String/match) がどのように動作するのかを指定します。さらに、これが存在するかどうかが、そのオブジェクトが正規表現とみなされるかどうかにも影響します。 {{InteractiveExample("JavaScript デモ: RegExp.prototype[Symbol.match]()")}} @@ -22,8 +21,8 @@ class RegExp1 extends RegExp { } } -console.log("2012-07-02".match(new RegExp1("([0-9]+)-([0-9]+)-([0-9]+)"))); -// Expected output: "VALID" +console.log("2012-07-02".match(new RegExp1("(\\d+)-(\\d+)-(\\d+)"))); +// 予想される結果: "VALID" ``` ## 構文 @@ -96,7 +95,7 @@ console.log("😄".match(/(?:)/gu)); // [ '', '' ] このメソッドは、_ほとんど_ {{jsxref("String.prototype.match()")}} と同じ方法で使用することができますが、 `this` と引数の並び順が異なります。 ```js -const re = /[0-9]+/g; +const re = /\d+/g; const str = "2016-01-02"; const result = re[Symbol.match](str); console.log(result); // ["2016", "01", "02"] @@ -119,7 +118,7 @@ class MyRegExp extends RegExp { } } -const re = new MyRegExp("([0-9]+)-([0-9]+)-([0-9]+)"); +const re = new MyRegExp("(\\d+)-(\\d+)-(\\d+)"); const str = "2016-01-02"; const result = str.match(re); // String.prototype.match は re[Symbol.match]() を呼び出す console.log(result.group(1)); // 2016 diff --git a/files/ja/web/javascript/reference/global_objects/regexp/symbol.matchall/index.md b/files/ja/web/javascript/reference/global_objects/regexp/symbol.matchall/index.md index 4ec6c8f4af75c6..3424d9173d88b3 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/symbol.matchall/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/symbol.matchall/index.md @@ -1,12 +1,11 @@ --- title: RegExp.prototype[Symbol.matchAll]() +short-title: "[Symbol.matchAll]()" slug: Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.matchAll l10n: - sourceCommit: 6fbdb78c1362fae31fbd545f4b2d9c51987a6bca + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`[Symbol.matchAll]()`** は {{jsxref("RegExp")}} インスタンスのメソッドで、 [`String.prototype.matchAll`](/ja/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll) がどのように動作するのかを指定します。 {{InteractiveExample("JavaScript デモ: RegExp.prototype[Symbol.matchAll]()", "taller")}} @@ -22,9 +21,9 @@ class MyRegExp extends RegExp { } } -const re = new MyRegExp("-[0-9]+", "g"); +const re = new MyRegExp("-\\d+", "g"); console.log("2016-01-02|2019-03-07".matchAll(re)); -// Expected output: Array [Array ["-01"], Array ["-02"], Array ["-03"], Array ["-07"]] +// 予想される結果: Array [Array ["-01"], Array ["-02"], Array ["-03"], Array ["-07"]] ``` ## 構文 @@ -90,7 +89,7 @@ console.log(Array.from("😄".matchAll(/(?:)/gu))); このメソッドは {{jsxref("String.prototype.matchAll()")}}, とほぼ同様に使用することができますが、 `this` の値と引数の順序が違う点が異なります。 ```js -const re = /[0-9]+/g; +const re = /\d+/g; const str = "2016-01-02"; const result = re[Symbol.matchAll](str); @@ -112,7 +111,7 @@ class MyRegExp extends RegExp { } } -const re = new MyRegExp("([0-9]+)-([0-9]+)-([0-9]+)", "g"); +const re = new MyRegExp("(\\d+)-(\\d+)-(\\d+)", "g"); const str = "2016-01-02|2019-03-07"; const result = str.matchAll(re); @@ -134,6 +133,7 @@ console.log(result[1]); ## 関連情報 - [`RegExp.prototype[Symbol.matchAll]` のポリフィル (`core-js`)](https://github.com/zloirock/core-js#ecmascript-string-and-regexp) +- [es-shims による `RegExp.prototype[Symbol.matchAll]` のポリフィル](https://www.npmjs.com/package/string.prototype.matchall) - {{jsxref("String.prototype.matchAll()")}} - [`RegExp.prototype[Symbol.match]()`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.match) - [`RegExp.prototype[Symbol.replace]()`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.replace) diff --git a/files/ja/web/javascript/reference/global_objects/regexp/symbol.replace/index.md b/files/ja/web/javascript/reference/global_objects/regexp/symbol.replace/index.md index fe84fa085194ef..8540f24cb7397d 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/symbol.replace/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/symbol.replace/index.md @@ -1,15 +1,16 @@ --- title: RegExp.prototype[Symbol.replace]() +short-title: "[Symbol.replace]()" slug: Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.replace l10n: - sourceCommit: 6fbdb78c1362fae31fbd545f4b2d9c51987a6bca + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`[Symbol.replace]()`** は {{jsxref("RegExp")}} インスタンスのメソッドで、正規表現がパターンとして渡されたときに [`String.prototype.replace()`](/ja/docs/Web/JavaScript/Reference/Global_Objects/String/replace) および[`String.prototype.replaceAll()`](/ja/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll) がどのように動作するかを指定します。 -{{InteractiveExample("JavaScript デモ: RegExp.prototype[SYmbol.replace]()")}} +{{InteractiveExample("JavaScript デモ: RegExp.prototype[Symbol.replace]()")}} + + ```js interactive-example class RegExp1 extends RegExp { @@ -19,7 +20,7 @@ class RegExp1 extends RegExp { } console.log("football".replace(new RegExp1("foo"))); -// Expected output: "#!@?tball" +// 予想される結果: "#!@?tball" ``` ## 構文 @@ -93,8 +94,8 @@ console.log("😄".replace(/(?:)/gu, " ")); // " 😄 " ```js const re = /-/g; const str = "2016-01-01"; -const newstr = re[Symbol.replace](str, "."); -console.log(newstr); // 2016.01.01 +const newStr = re[Symbol.replace](str, "."); +console.log(newStr); // 2016.01.01 ``` ### サブクラスでの `[Symbol.replace]()` の使用 @@ -119,8 +120,8 @@ class MyRegExp extends RegExp { const re = new MyRegExp("\\d", "", 3); const str = "01234567"; -const newstr = str.replace(re, "#"); // String.prototype.replace は re[Symbol.replace]() を呼び出す -console.log(newstr); // ###34567 +const newStr = str.replace(re, "#"); // String.prototype.replace は re[Symbol.replace]() を呼び出す +console.log(newStr); // ###34567 ``` ## 仕様書 diff --git a/files/ja/web/javascript/reference/global_objects/regexp/symbol.search/index.md b/files/ja/web/javascript/reference/global_objects/regexp/symbol.search/index.md index ae5aaefe60cd1e..56c6213e078183 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/symbol.search/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/symbol.search/index.md @@ -1,12 +1,11 @@ --- title: RegExp.prototype[Symbol.search]() +short-title: "[Symbol.search]()" slug: Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.search l10n: - sourceCommit: 6fbdb78c1362fae31fbd545f4b2d9c51987a6bca + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`[Symbol.search]()`** は {{jsxref("RegExp")}} インスタンスのメソッドで、 [`String.prototype.search`](/ja/docs/Web/JavaScript/Reference/Global_Objects/String/search) がどのように動作するのかを指定します。 {{InteractiveExample("JavaScript デモ: RegExp.prototype[Symbol.search]()")}} @@ -23,7 +22,7 @@ class RegExp1 extends RegExp { } console.log("table football".search(new RegExp1("foo"))); -// Expected output: 6 +// 予想される結果: 6 ``` ## 構文 diff --git a/files/ja/web/javascript/reference/global_objects/regexp/symbol.split/index.md b/files/ja/web/javascript/reference/global_objects/regexp/symbol.split/index.md index d6bafcb6cc89a4..3436321f6aa4d6 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/symbol.split/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/symbol.split/index.md @@ -1,12 +1,11 @@ --- title: RegExp.prototype[Symbol.split]() +short-title: "[Symbol.split]()" slug: Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.split l10n: - sourceCommit: 6fbdb78c1362fae31fbd545f4b2d9c51987a6bca + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`[Symbol.split]()`** は {{jsxref("RegExp")}} インスタンスのメソッドで、 [`String.prototype.split`](/ja/docs/Web/JavaScript/Reference/Global_Objects/String/split) にセパレーターとして正規表現が渡されたときにどのように動作するのかを指定します。 {{InteractiveExample("JavaScript デモ: RegExp.prototype[Symbol.split]()")}} @@ -20,10 +19,10 @@ class RegExp1 extends RegExp { } console.log("2016-01-02".split(new RegExp1("-"))); -// Expected output: Array ["(2016)", "(01)", "(02)"] +// 予想される結果: Array ["(2016)", "(01)", "(02)"] -console.log("2016-01-02".split(new RegExp("-"))); -// Expected output: Array ["2016", "01", "02"] +console.log("2016-01-02".split(/-/)); +// 予想される結果: Array ["2016", "01", "02"] ``` ## 構文 diff --git a/files/ja/web/javascript/reference/global_objects/regexp/test/index.md b/files/ja/web/javascript/reference/global_objects/regexp/test/index.md index 08d85cfaab2f28..cd8e0f61051f15 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/test/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/test/index.md @@ -1,12 +1,11 @@ --- title: RegExp.prototype.test() +short-title: test() slug: Web/JavaScript/Reference/Global_Objects/RegExp/test l10n: - sourceCommit: 5bdcf72ed6ffc7d4fa878060a548869ed6ae149b + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`test()`** は {{jsxref("RegExp")}} インスタンスのメソッドで、正規表現と指定された文字列を照合するための検索を実行します。一致があった場合は `true` を、それ以外の場合は `false` を返します。 JavaScript の {{jsxref("RegExp")}} オブジェクトは {{jsxref("RegExp/global", "global")}} または {{jsxref("RegExp/sticky", "sticky")}} フラグ(`/foo/g` や `/foo/y` など)を設定すると**ステートフル**になります。これらは前回一致したときの {{jsxref("RegExp/lastIndex", "lastIndex")}} を格納します。これを内部的に使用することで、 `test()` を使用して文字列の複数の照合を反復処理することができます(キャプチャグループを使用)。 @@ -20,19 +19,19 @@ const regex = new RegExp("foo*"); const globalRegex = new RegExp("foo*", "g"); console.log(regex.test(str)); -// Expected output: true +// 予想される結果: true console.log(globalRegex.lastIndex); -// Expected output: 0 +// 予想される結果: 0 console.log(globalRegex.test(str)); -// Expected output: true +// 予想される結果: true console.log(globalRegex.lastIndex); -// Expected output: 9 +// 予想される結果: 9 console.log(globalRegex.test(str)); -// Expected output: false +// 予想される結果: false ``` ## 構文 diff --git a/files/ja/web/javascript/reference/global_objects/regexp/unicode/index.md b/files/ja/web/javascript/reference/global_objects/regexp/unicode/index.md index 0fb7df05bea4f2..3e03335198a1e2 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/unicode/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/unicode/index.md @@ -1,31 +1,24 @@ --- title: RegExp.prototype.unicode +short-title: unicode slug: Web/JavaScript/Reference/Global_Objects/RegExp/unicode l10n: - sourceCommit: fc67640f3545c1a5db42c878d1f0de71313349bc + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`unicode`** は {{jsxref("RegExp")}} インスタンスのアクセサープロパティで、この正規表現に `u` フラグが使用されているかどうかを返します。 {{InteractiveExample("JavaScript デモ: RegExp.prototype.unicode", "taller")}} ```js interactive-example -const regex1 = new RegExp("\u{61}"); -const regex2 = new RegExp("\u{61}", "u"); +const regex1 = /\u{61}/; +const regex2 = /\u{61}/u; console.log(regex1.unicode); -// Expected output: false +// 予想される結果: false console.log(regex2.unicode); -// Expected output: true - -console.log(regex1.source); -// Expected output: "a" - -console.log(regex2.source); -// Expected output: "a" +// 予想される結果: true ``` ## 解説 @@ -68,10 +61,10 @@ console.log(regex.unicode); // true ## 関連情報 - {{jsxref("RegExp.prototype.lastIndex")}} -- {{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.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")}} diff --git a/files/ja/web/javascript/reference/global_objects/regexp/unicodesets/index.md b/files/ja/web/javascript/reference/global_objects/regexp/unicodesets/index.md index 999e2680b5c874..3270ecbdd4ceaa 100644 --- a/files/ja/web/javascript/reference/global_objects/regexp/unicodesets/index.md +++ b/files/ja/web/javascript/reference/global_objects/regexp/unicodesets/index.md @@ -1,14 +1,26 @@ --- title: RegExp.prototype.unicodeSets +short-title: unicodeSets slug: Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets l10n: - sourceCommit: 3c33463072905e81ac620dd9780313369029b498 + sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166 --- -{{JSRef}} - **`unicodeSets`** は {{jsxref("RegExp")}} インスタンスのアクセサープロパティで、この正規表現に `v` フラグが使用されているかどうかを返します。 +{{InteractiveExample("JavaScript デモ: RegExp.prototype.unicodeSets")}} + +```js interactive-example +const regex1 = /[\p{Lowercase}&&\p{Script=Greek}]/; +const regex2 = /[\p{Lowercase}&&\p{Script=Greek}]/v; + +console.log(regex1.unicodeSets); +// 予想される結果: false + +console.log(regex2.unicodeSets); +// 予想される結果: true +``` + ## 解説 `RegExp.prototype.unicodeSets` は `v` フラグが使用されている場合は `true` を、そうでない場合は `false` を返します。`v` フラグは [`u`](/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode) フラグを「アップグレード」したもので、Unicode 関連の機能をより有効にするものです(`u` と `v` は同じ正規表現を互換性のない方法で解釈するので、両方のフラグを使用すると {{jsxref("SyntaxError")}} になります。)`v` フラグを使用すると、`u` フラグの説明で述べたすべての機能に加えて、以下の機能が得られます。 @@ -45,12 +57,12 @@ console.log(regex.unicodeSets); // true ## 関連情報 - {{jsxref("RegExp.prototype.lastIndex")}} -- {{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")}} - [RegExp v flag with set notation and properties of strings](https://v8.dev/features/regexp-v-flag) on v8.dev (2022)