diff options
Diffstat (limited to 'files/ja/web/javascript/reference/global_objects/regexp')
28 files changed, 2693 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/global_objects/regexp/@@match/index.html b/files/ja/web/javascript/reference/global_objects/regexp/@@match/index.html new file mode 100644 index 0000000000..4359a94e96 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/@@match/index.html @@ -0,0 +1,116 @@ +--- +title: 'RegExp.prototype[@@match]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@match +tags: + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - Regular Expression +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@match +--- +<div>{{JSRef}}</div> + +<p><strong><code>[@@match]()</code></strong> メソッドは、<em>文字列</em>の<em>正規表現</em>に一致した部分を取得します。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-@@match.html")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox notranslate"><var>regexp</var>[Symbol.match](<var>str</var>)</pre> + +<h3 id="Parameters" name="Parameters">引数</h3> + +<dl> + <dt><code><var>str</var></code></dt> + <dd>一致の対象となる {{jsxref("String")}}。</dd> +</dl> + +<h3 id="Return_value" name="Return_value">返値</h3> + +<p>一致した全体の結果と括弧が捕捉した一致箇所の結果を含む {{jsxref("Array")}}。一致するものがなかった場合は、{{jsxref("null")}}。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p>このメソッドは、{{jsxref("String.prototype.match()")}} で内部的に呼び出されます。</p> + +<p>たとえば、次の 2 つの例は同じ結果を返します。</p> + +<pre class="brush: js notranslate">'abc'.match(/a/); + +/a/[Symbol.match]('abc');</pre> + +<p>このメソッドは、<code>RegExp</code> サブクラス内で一致の振る舞いをカスタマイズするために存在しています。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Direct_call" name="Direct_call">直接呼び出し</h3> + +<p>このメソッドは、<em>ほとんど</em> {{jsxref("String.prototype.match()")}} と同じ方法で使用することができますが、 <code>this</code> と引数の並び順が異なります。</p> + +<pre class="brush: js notranslate">let re = /[0-9]+/g; +let str = '2016-01-02'; +let result = re[Symbol.match](str); +console.log(result); // ["2016", "01", "02"] +</pre> + +<h3 id="Using_match_in_subclasses" name="Using_match_in_subclasses">サブクラスで @@match を使用</h3> + +<p>{{jsxref("RegExp")}} のサブクラスは、既定の動作を修正するために <code>[@@match]()</code> メソッドをオーバーライドできます。</p> + +<pre class="brush: js notranslate">class MyRegExp extends RegExp { + [Symbol.match](str) { + let result = RegExp.prototype[Symbol.match].call(this, str); + if (!result) return null; + return { + group(n) { + return result[n]; + } + }; + } +} + +let re = new MyRegExp('([0-9]+)-([0-9]+)-([0-9]+)'); +let str = '2016-01-02'; +let result = str.match(re); // String.prototype.match calls re[@@match]. +console.log(result.group(1)); // 2016 +console.log(result.group(2)); // 01 +console.log(result.group(3)); // 02 +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@match', 'RegExp.prototype[@@match]')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.@@match")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("String.prototype.match()")}}</li> + <li>{{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@search()", "RegExp.prototype[@@search]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</li> + <li>{{jsxref("RegExp.prototype.exec()")}}</li> + <li>{{jsxref("RegExp.prototype.test()")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/@@matchall/index.html b/files/ja/web/javascript/reference/global_objects/regexp/@@matchall/index.html new file mode 100644 index 0000000000..6c85b1921e --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/@@matchall/index.html @@ -0,0 +1,112 @@ +--- +title: 'RegExp.prototype[@@matchAll]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll +tags: + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - Regular Expressions + - メソッド + - 正規表現 +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll +--- +<div>{{JSRef}}</div> + +<p><strong><code>[@@matchAll]</code></strong> メソッドは、文字列に対する正規表現で一致するすべてのものを返します。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-@@matchall.html", "taller")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox notranslate"><var>regexp</var>[Symbol.matchAll](<var>str</var>)</pre> + +<h3 id="Parameters" name="Parameters">引数</h3> + +<dl> + <dt><code><var>str</var></code></dt> + <dd>一致の対象となる {{jsxref("String")}} です。</dd> +</dl> + +<h3 id="Return_value" name="Return_value">返値</h3> + +<p><a href="/ja/docs/Web/JavaScript/Guide/Iterators_and_Generators">イテレーター</a>です。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p>このメソッドは内部的に {{jsxref("String.prototype.matchAll()")}} を呼び出します。例えば、以下の2つの例は同じ結果を返します。</p> + +<pre class="brush: js notranslate">'abc'.matchAll(/a/); + +/a/[Symbol.matchAll]('abc');</pre> + +<p>このメソッドは <code>matchAll()</code> の動作を {{jsxref('RegExp')}} のサブクラスの中でカスタマイズするために存在します。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Direct_call" name="Direct_call">直接呼び出し</h3> + +<p>このメソッドは {{jsxref("String.prototype.matchAll()")}}, とほぼ同様に使用することができますが、 <code>this</code> の値と引数の順序が違う点が異なります。</p> + +<pre class="brush: js notranslate">let re = /[0-9]+/g; +let str = '2016-01-02'; +let result = re[Symbol.matchAll](str); + +console.log(Array.from(result, x => x[0])); +// ["2016", "01", "02"] +</pre> + +<h3 id="Using_matchAll_in_subclasses" name="Using_matchAll_in_subclasses">サブクラスでの @@matchAll の使用</h3> + +<p>{{jsxref("RegExp")}} のサブクラスは <code>[@@matchAll]()</code> メソッドを上書きして既定の動作を変更することができます。</p> + +<p>例えば、 {{jsxref("Array")}} を<a href="/ja/docs/Web/JavaScript/Guide/Iterators_and_Generators">イテレーター</a>の代わりに返すことができます。</p> + +<pre class="brush: js notranslate">class MyRegExp extends RegExp { + [Symbol.matchAll](str) { + const result = RegExp.prototype[Symbol.matchAll].call(this, str); + if (!result) { + return null; + } else { + return Array.from(result); + } + } +} + +const re = new MyRegExp('([0-9]+)-([0-9]+)-([0-9]+)', 'g'); +const str = '2016-01-02|2019-03-07'; +const result = str.matchAll(re); +console.log(result[0]); // [ "2016-01-02", "2016", "01", "02" ] +console.log(result[1]); // [ "2019-03-07", "2019", "03", "07" ] +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp-prototype-matchall', 'RegExp.prototype[@@matchAll]')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.@@matchAll")}}</p> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{JSxRef("String.prototype.matchAll()")}}</li> + <li>{{JSxRef("Symbol.matchAll")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/@@replace/index.html b/files/ja/web/javascript/reference/global_objects/regexp/@@replace/index.html new file mode 100644 index 0000000000..3383b052d3 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/@@replace/index.html @@ -0,0 +1,120 @@ +--- +title: 'RegExp.prototype[@@replace]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@replace +tags: + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - Regular Expression +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@replace +--- +<div>{{JSRef}}</div> + +<p><strong><code>[@@replace]()</code></strong> メソッドは文字列内の <code>this</code> パターンの一部または、すべての一致箇所を <code>replacement</code> で置き換え、置換結果を新しい文字列として返します。 <code>replacement</code> は文字列にするか、関数にしてすべての一致箇所ごとに呼び出されるようにすることができます。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-@@replace.html")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox notranslate"><var>regexp</var>[Symbol.replace](<var>str<var>, <var>newSubStr</var>|<var>function</var>)</var></var></pre> + +<h3 id="Parameters" name="Parameters">引数</h3> + +<dl> + <dt><code><var>str</var></code></dt> + <dd>置換の対象となる {{jsxref("String")}} です。</dd> + <dt><code><var>newSubStr</var></code> (置換内容)</dt> + <dd>部分文字列を置き換える {{jsxref("String")}} です。特殊な置換パターンの数値に対応しています。 {{jsxref("String.prototype.replace()")}} ページの{{jsxref("String.prototype.replace", "引数としての文字列の指定", "#Specifying_a_string_as_a_parameter", 1)}}の節を参照してください。</dd> + <dt><code><var>function</var></code> (置換内容)</dt> + <dd>新しい部分文字列を生成するために呼び出される関数。この関数に提供される引数については、{{jsxref("String.prototype.replace()")}} ページの{{jsxref("String.prototype.replace", "引数としての関数の指定", "#Specifying_a_function_as_a_parameter", 1)}}の節を見てください。</dd> +</dl> + +<h3 id="Return_value" name="Return_value">返値</h3> + +<p>一部またはすべてのパターンの一致箇所が置換内容によって置き換えられた新しい文字列です。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p>このメソッドは {{jsxref("String.prototype.replace()")}} の中で、 <code>pattern</code> 引数が {{jsxref("RegExp")}} オブジェクトであった場合に内部的に呼び出されます。たとえば、次の 2 つの例は同じ結果を返します。</p> + +<pre class="brush: js notranslate">'abc'.replace(/a/, 'A'); + +/a/[Symbol.replace]('abc', 'A');</pre> + +<p>このメソッドは、 <code>RegExp</code> サブクラスの置換動作をカスタマイズするために存在しています。</p> + +<p>{{jsxref("String.prototype.replace()")}} は <code>pattern</code> 引数が {{jsxref("RegExp")}} オブジェクトでは<strong>ない</strong>場合、このメソッドの呼び出しや {{jsxref("RegExp")}} オブジェクトの生成を行いません。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Direct_call" name="Direct_call">直接呼出し</h3> + +<p><code>this</code> と引数の順序が異なる点を除いて、このメソッドは {{jsxref("String.prototype.replace()")}} とほどんど同じ使い方ができます。</p> + +<pre class="brush: js notranslate">var re = /-/g; +var str = '2016-01-01'; +var newstr = re[Symbol.replace](str, '.'); +console.log(newstr); // 2016.01.01 +</pre> + +<h3 id="Using_replace_in_subclasses" name="Using_replace_in_subclasses">サブクラスでの @@replace の使用</h3> + +<p>既定の動作を修正するために、{{jsxref("RegExp")}} のサブクラスで <code>[@@replace]()</code> メソッドをオーバーライドできます。</p> + +<pre class="brush: js notranslate">class MyRegExp extends RegExp { + constructor(pattern, flags, count) { + super(pattern, flags); + this.count = count; + } + [Symbol.replace](str, replacement) { + // Perform @@replace |count| times. + var result = str; + for (var i = 0; i < this.count; i++) { + result = RegExp.prototype[Symbol.replace].call(this, result, replacement); + } + return result; + } +} + +var re = new MyRegExp('\\d', '', 3); +var str = '01234567'; +var newstr = str.replace(re, '#'); // String.prototype.replace calls re[@@replace]. +console.log(newstr); // ###34567</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@replace', 'RegExp.prototype[@@replace]')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.@@replace")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("String.prototype.replace()")}}</li> + <li>{{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@search()", "RegExp.prototype[@@search]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</li> + <li>{{jsxref("RegExp.prototype.exec()")}}</li> + <li>{{jsxref("RegExp.prototype.test()")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/@@search/index.html b/files/ja/web/javascript/reference/global_objects/regexp/@@search/index.html new file mode 100644 index 0000000000..2f4449ab2e --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/@@search/index.html @@ -0,0 +1,113 @@ +--- +title: 'RegExp.prototype[@@search]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@search +tags: + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - Regular Expression +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@search +--- +<div>{{JSRef}}</div> + +<p><strong><code>[@@search]()</code></strong> メソッドは、<code>this</code> 正規表現と文字列の間で一致させるための検索を実行します。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-@@search.html")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox notranslate"><var>regexp</var>[Symbol.search](<var>str</var>)</pre> + +<h3 id="Parameters" name="Parameters">引数</h3> + +<dl> + <dt><code><var>str</var></code></dt> + <dd>検索対象の {{jsxref("String")}} です。</dd> +</dl> + +<h3 id="Return_value" name="Return_value">返値</h3> + +<dl> + <dt>integer</dt> + <dd>成功した場合、<code>[@@search]()</code> は文字列内で正規表現に最初に一致したインデックスを返します。そうではない場合、-1 を返します。</dd> +</dl> + +<h2 id="Description" name="Description">解説</h2> + +<p>このメソッドは、 {{jsxref("String.prototype.search()")}} の内部で呼び出されます。たとえば、次の 2 つの例は同じ結果を返します。</p> + +<pre class="brush: js notranslate">'abc'.search(/a/); + +/a/[Symbol.search]('abc');</pre> + +<p>このメソッドは、<code>RegExp</code> サブクラスで検索動作をカスタマイズするために存在しています。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Direct_call" name="Direct_call">直接呼出し</h3> + +<p>このメソッドは、<code>this</code> と引数順が異なることを除いて {{jsxref("String.prototype.search()")}} とほぼ同じ方法で使用できます。</p> + +<pre class="brush: js notranslate">var re = /-/g; +var str = '2016-01-02'; +var result = re[Symbol.search](str); +console.log(result); // 4 +</pre> + +<h3 id="Using_search_in_subclasses" name="Using_search_in_subclasses">サブクラスでの @@search の使用</h3> + +<p>{{jsxref("RegExp")}} のサブクラスは、動作を修正するために <code>[@@search]()</code> メソッドをオーバーライドできます。</p> + +<pre class="brush: js notranslate">class MyRegExp extends RegExp { + constructor(str) { + super(str) + this.pattern = str; + } + [Symbol.search](str) { + return str.indexOf(this.pattern); + } +} + +var re = new MyRegExp('a+b'); +var str = 'ab a+b'; +var result = str.search(re); // String.prototype.search は再定義した [@@search] を呼び出す。 +console.log(result); // 3 +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@search', 'RegExp.prototype[@@search]')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.@@search")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("String.prototype.search()")}}</li> + <li>{{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</li> + <li>{{jsxref("RegExp.prototype.exec()")}}</li> + <li>{{jsxref("RegExp.prototype.test()")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/@@species/index.html b/files/ja/web/javascript/reference/global_objects/regexp/@@species/index.html new file mode 100644 index 0000000000..440ca4aeec --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/@@species/index.html @@ -0,0 +1,70 @@ +--- +title: 'get RegExp[@@species]' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@species +tags: + - JavaScript + - Property + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@species +--- +<div>{{JSRef}}</div> + +<p><strong><code>RegExp[@@species]</code></strong> アクセサープロパティは <code>RegExp</code> コンストラクターを返します。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-getregexp-@@species.html")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>species</code> アクセサープロパティは、 <code>RegExp</code> の既定のコンストラクターを返します。サブクラスのコンストラクターは、コンストラクターの割り当てをオーバーライドするためにこれを使用することができます。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Species_in_ordinary_objects" name="Species_in_ordinary_objects">通常オブジェクトの species</h3> + +<p><code>species</code> プロパティは、既定のコンストラクターを返します。 <code>RegExp</code> オブジェクトであれば、 <code>RegExp</code> コンストラクターを返します。</p> + +<pre class="brush: js notranslate">RegExp[Symbol.species]; // function RegExp()</pre> + +<h3 id="Species_in_derived_objects" name="Species_in_derived_objects">派生オブジェクトの species</h3> + +<p>派生クラスのコレクションオブジェクト (たとえば、独自の正規表現を表す <code>MyRegExp</code>) では、 <code>MyRegExp</code> の species は <code>MyRegExp</code> コンストラクターです。しかし、派生クラスのメソッドで親である <code>RegExp</code> オブジェクトを返すようにこれをオーバーライドしたくなるかもしれません。</p> + +<pre class="brush: js notranslate">class MyRegExp extends RegExp { + // MyRegExp の species を親である RegExp コンストラクタにオーバーライドします。 + static get [Symbol.species]() { return RegExp; } +}</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp-@@species', 'get RegExp [ @@species ]')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.@@species")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp")}}</li> + <li>{{jsxref("Symbol.species")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/@@split/index.html b/files/ja/web/javascript/reference/global_objects/regexp/@@split/index.html new file mode 100644 index 0000000000..ac70871fde --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/@@split/index.html @@ -0,0 +1,111 @@ +--- +title: 'RegExp.prototype[@@split]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@split +tags: + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@split +--- +<div>{{JSRef}}</div> + +<p><strong><code>[@@split]()</code></strong> メソッドは、文字列を部分文字列に区切ることによって、 {{jsxref("String")}} オブジェクトを文字列の配列に分割します。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-@@split.html")}}</div> + +<p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox notranslate"><var>regexp</var>[Symbol.split](<var>str</var>[, <var>limit</var>])</pre> + +<h3 id="Parameters" name="Parameters">引数</h3> + +<dl> + <dt><code><var>str</var></code></dt> + <dd>分割操作の対象。</dd> + <dt><code><var>limit</var></code> {{optional_inline}}</dt> + <dd> + <p>検出される分割数の制限を指定する整数。 <code>[@@split]()</code> メソッドは、 <code>this</code> RegExp パターン (または上記の構文では <code><var>regexp</var></code>) に一致するたびに、分割項目の数が <code><var>limit</var></code> と一致するか、文字列が <code>this</code> パターンを満たなくなるまで、分割を行います。</p> + </dd> +</dl> + +<h3 id="Return_value" name="Return_value">返値</h3> + +<p>要素として部分文字列を含む {{jsxref("Array")}}。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p>このメソッドは {{jsxref("String.prototype.split()")}} の内部で、 <code>separator</code> 引数が <code>@@split</code> メソッドを持つオブジェクト、たとえば {{jsxref("RegExp")}} オブジェクトだった場合に呼び出されます。たとえば、次の 2 つの例は同じ結果を返します。</p> + +<pre class="brush: js notranslate">'a-b-c'.split(/-/); + +/-/[Symbol.split]('a-b-c');</pre> + +<p>このメソッドは、<code>RegExp</code> のサブクラスで <code>split()</code> の動作をカスタマイズするために存在します。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Direct_call" name="Direct_call">直接呼出し</h3> + +<p><code>this</code> の扱いと引数の並び順を除いて、このメソッドは {{jsxref("String.prototype.split()")}} とほとんど同じように使用できます。</p> + +<pre class="brush: js notranslate">let re = /-/g; +let str = '2016-01-02'; +let result = re[Symbol.split](str); +console.log(result); // ["2016", "01", "02"] +</pre> + +<h3 id="Using_split_in_subclasses" name="Using_split_in_subclasses">サブクラスで @@split を使用する</h3> + +<p>既定の動作を修正するために、{{jsxref("RegExp")}} のサブクラスで <code>[@@split]()</code> メソッドをオーバーライドできます。</p> + +<pre class="brush: js notranslate">class MyRegExp extends RegExp { + [Symbol.split](str, limit) { + let result = RegExp.prototype[Symbol.split].call(this, str, limit); + return result.map(x => "(" + x + ")"); + } +} + +let re = new MyRegExp('-'); +let str = '2016-01-02'; +let result = str.split(re); // String.prototype.split calls re[@@split]. +console.log(result); // ["(2016)", "(01)", "(02)"] +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@split', 'RegExp.prototype[@@split]')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.@@split")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("String.prototype.split()")}}</li> + <li>{{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@search()", "RegExp.prototype[@@search]()")}}</li> + <li>{{jsxref("RegExp.prototype.exec()")}}</li> + <li>{{jsxref("RegExp.prototype.test()")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/compile/index.html b/files/ja/web/javascript/reference/global_objects/regexp/compile/index.html new file mode 100644 index 0000000000..09017079c6 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/compile/index.html @@ -0,0 +1,85 @@ +--- +title: RegExp.prototype.compile() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/compile +tags: + - Deprecated + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/compile +--- +<div>{{JSRef}} {{deprecated_header}}</div> + +<p>非推奨の <strong><code>compile()</code></strong> メソッドは、スクリプトの実行中に正規表現を(再)コンパイルするために使われます。基本的に <code>RegExp</code> コンストラクターと同じです。</p> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox notranslate"><var>regexObj</var>.compile(<var>pattern</var>, <var>flags</var>)</pre> + +<h3 id="Parameters" name="Parameters">引数</h3> + +<dl> + <dt><code><var>pattern</var></code></dt> + <dd>正規表現のテキスト</dd> + <dt><code><var>flags</var></code></dt> + <dd> + <p>指定された場合、フラグは次の値の任意の組み合わせになります。</p> + + <dl> + <dt><code>g</code></dt> + <dd>グローバルマッチ</dd> + <dt><code>i</code></dt> + <dd>大文字小文字の違いを無視</dd> + <dt><code>m</code></dt> + <dd>複数行。始まりと終わりの文字 (^ と $) を複数行にわたって動作するものとして扱います。 (すなわち、 <em>それぞれ</em>の行の始まりと終わりにマッチします。(\n または \r によって区切られます)、入力文字列全体の始まりと終わりだけではありません。)</dd> + <dt><code>y</code></dt> + <dd>先頭固定。対象文字列においてこの正規表現の <code>lastIndex</code> プロパティによって示されるインデックスからのみ検索します (それ以降のインデックスから検索しようとはしません)。</dd> + </dl> + </dd> +</dl> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>compile</code> メソッドは非推奨です。同じ効果を得るには、 <code>RegExp</code> コンストラクターを使用してください。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_compile" name="Using_compile">compile() の使用</h3> + +<p>次の例では、新しいパターンとフラグで正規表現を再コンパイルする方法を示します。</p> + +<pre class="brush: js notranslate">var regexObj = new RegExp('foo', 'gi'); +regexObj.compile('new foo', 'g'); +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype.compile', 'RegExp.prototype.compile')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.compile")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/dotall/index.html b/files/ja/web/javascript/reference/global_objects/regexp/dotall/index.html new file mode 100644 index 0000000000..b7e18ebf2d --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/dotall/index.html @@ -0,0 +1,87 @@ +--- +title: RegExp.prototype.dotAll +slug: Web/JavaScript/Reference/Global_Objects/RegExp/dotAll +tags: + - Draft + - JavaScript + - Property + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/dotAll +--- +<p>{{JSRef}}</p> + +<p><strong><code>dotAll</code></strong> プロパティは、正規表現で "<code>s</code>" フラグが使用されているかどうかを示します。 <code>dotAll</code> は、個々の正規表現インスタンスの読み取り専用プロパティです。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-dotall.html")}}</div> + +<p>{{JS_Property_Attributes(0, 0, 1)}}</p> + +<h2 id="説明">説明</h2> + +<p>dotAllの値は {{JSxRef("Boolean")}} であり、"<code>s</code>" フラグが使用されている場合は <code>true</code> 、それ以外の場合は <code>false</code> です。 "<code>s</code>" フラグは、ドット特殊文字 ("<code>.</code>") が以下に示す行末記号 ("<code>newline</code>") 文字と一致することを示します。これ以外の場合は一致しません:</p> + +<ul> + <li>U+000A 改行 (LF) ("<code>\n</code>")</li> + <li>U+000D キャリッジリターン (CR) ("<code>\r</code>")</li> + <li>U+2028 ラインセパレーター</li> + <li>U+2029 段落区切り文字</li> +</ul> + +<p>これは事実上、ドットが基本多言語面 (BMP) のすべての文字と一致することを意味します。 アストラル文字と一致させるには、"<code>u</code>" (ユニコード) フラグを使用する必要があります。 両方のフラグを組み合わせて使用すると、ドットは例外なく任意のユニコード文字に一致します。</p> + +<p>このプロパティを直接変更することはできません。</p> + +<h2 id="例">例</h2> + +<h3 id="dotAllを使用する">dotAllを使用する</h3> + +<pre class="brush: js notranslate">var str1 = 'bar\nexample foo example'; + +var regex1 = new RegExp('bar.example','s'); + +console.log(regex1.dotAll); // Output: true + +console.log(str1.replace(regex1,'')); // Output: foo example + +var str2 = 'bar\nexample foo example'; + +var regex2 = new RegExp('bar.example'); + +console.log(regex2.dotAll); // Output: false + +console.log(str2.replace(regex2,'')); // Output: bar + // example foo example</pre> + +<h2 id="仕様書">仕様書</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">仕様書</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.dotAll', 'RegExp.prototype.dotAll')}}</td> + </tr> + </tbody> +</table> + +<h2 id="ブラウザーの互換性">ブラウザーの互換性</h2> + +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div> + +<p>{{Compat("javascript.builtins.RegExp.dotAll")}}</p> + +<h2 id="関連情報">関連情報</h2> + +<ul> + <li>{{JSxRef("RegExp.lastIndex")}}</li> + <li>{{JSxRef("RegExp.prototype.global")}}</li> + <li>{{JSxRef("RegExp.prototype.ignoreCase")}}</li> + <li>{{JSxRef("RegExp.prototype.multiline")}}</li> + <li>{{JSxRef("RegExp.prototype.source")}}</li> + <li>{{JSxRef("RegExp.prototype.sticky")}}</li> + <li>{{JSxRef("RegExp.prototype.unicode")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/exec/index.html b/files/ja/web/javascript/reference/global_objects/regexp/exec/index.html new file mode 100644 index 0000000000..058f1c3cc7 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/exec/index.html @@ -0,0 +1,198 @@ +--- +title: RegExp.prototype.exec() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/exec +tags: + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/exec +--- +<div>{{JSRef}}</div> + +<p><span class="seoSummary"><strong><code>exec()</code></strong> メソッドは、指定された文字列内で一致するものの検索を実行します。結果の配列、または {{jsxref("null")}} を返します。</span></p> + +<p>JavaScript の {{jsxref("RegExp")}} オブジェクトは、 {{jsxref("RegExp.global", "global")}} または {{jsxref("RegExp.sticky", "sticky")}} フラグが設定されている場合 (例えば <code>/foo/g</code> や <code>/foo/y</code>) は<strong>ステートフル</strong>になります。これは前回の一致位置を {{jsxref("RegExp.lastIndex", "lastIndex")}} に格納します。これを内部的に使用することで、 <code>exec()</code> はテキストの文字列内で (キャプチャグループのある) 複数の一致を反復処理することができます。これは単なる文字列の一致を取得する {{jsxref("String.prototype.match()")}} とは対照的です。</p> + +<p>より新しい関数として、 (キャプチャグループによる) 複数の部分の文字列の一致を単純化する {{jsxref("String.prototype.matchAll()")}} が提案されています。</p> + +<p>単に見つかったか見つからなかったかを <code>true</code> または <code>false</code> で知るために検索を実行するのであれば、 {{jsxref("RegExp.prototype.test()")}} メソッドまたは {{jsxref("String.prototype.search()")}} メソッドを使用してください。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-exec.html")}}</div> + +<p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox notranslate"><var>regexObj</var>.exec(<var>str</var>)</pre> + +<h3 id="Parameters" name="Parameters">引数</h3> + +<dl> + <dt><code><var>str</var></code></dt> + <dd>正規表現に一致するかどうかの対象となる文字列。</dd> +</dl> + +<h3 id="Return_value" name="Return_value">返値</h3> + +<p>一致に成功した場合、 <code>exec()</code> メソッドは配列を返し (追加のプロパティ <code>index</code> と <code>input</code> 付き、以下参照)、正規表現オブジェクトの {{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティを更新します。返された配列は、一致したテキストを最初の項目として持ち、その後、一致したテキストの括弧によるキャプチャグループに対して 1 つずつの項目を持ちます。</p> + +<p>一致に失敗した場合は、 <code>exec()</code> メソッドは {{jsxref("null")}} を返し、 {{jsxref("RegExp.lastIndex", "lastIndex")}} を <code>0</code> に設定します。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p>次の例を想定してください。</p> + +<pre class="brush: js notranslate">// "quick brown" の後に "jumps" が来るものを、その間の文字を無視して一致させます。 +// "brown" と "jumps" を取得します。 +// 大文字と小文字は区別しません。 +let re = /quick\s(brown).+?(jumps)/ig; +let result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog'); +</pre> + +<p>このスクリプトの結果は以下の表の通りです。</p> + +<table class="fullwidth-table standard-table"> + <thead> + <tr> + <th scope="row">オブジェクト</th> + <th scope="col">プロパティ/添字</th> + <th scope="col">説明</th> + <th scope="col">例</th> + </tr> + </thead> + <tbody> + <tr> + <th colspan="1" rowspan="4" scope="row" style="vertical-align: top;"><code>result</code></th> + <td><code>[0]</code></td> + <td>文字が一致した部分の文字列全体</td> + <td><code>"Quick Brown Fox Jumps"</code></td> + </tr> + <tr> + <td><code>[1], ...[<var>n</var>]</code></td> + <td> + <p>もしあれば、括弧に囲まれた部分文字列が一致したものです。</p> + + <p>括弧に囲まれた部分文字列の数に制限はありません。</p> + </td> + <td> + <p><code>result[1] === "Brown"</code></p> + + <p><code>result[2] === "Jumps"</code></p> + </td> + </tr> + <tr> + <td><code>index</code></td> + <td>0 から始める一致した文字列の位置。</td> + <td><code>4</code></td> + </tr> + <tr> + <td><code>input</code></td> + <td>検索対象となった元の文字列。</td> + <td><code>The Quick Brown Fox Jumps Over The Lazy Dog</code></td> + </tr> + <tr> + <th colspan="1" rowspan="5" scope="row" style="vertical-align: top;"><code>re</code></th> + <td><code>lastIndex</code></td> + <td> + <p>次回の検索を始める位置です。</p> + </td> + <td><code>g</code> がない場合は <code>0</code> のままです。</td> + <td><code>25</code></td> + </tr> + <tr> + <td><code>ignoreCase</code></td> + <td>大文字小文字を区別しない、 <code>i</code> フラグが指定されているかどうか。</td> + <td><code>true</code></td> + </tr> + <tr> + <td><code>global</code></td> + <td>グローバルマッチのための、 <code>g</code> フラグが指定されているかどうか。</td> + <td><code>true</code></td> + </tr> + <tr> + <td><code>multiline</code></td> + <td>複数行に渡って文字列を検索する、 <code>m</code> フラグが指定されているかどうか。</td> + <td><code>false</code></td> + </tr> + <tr> + <td><code>source</code></td> + <td>パターンの文字列。</td> + <td><code>quick\s(brown).+?(jumps)</code></td> + </tr> + </tbody> +</table> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Finding_successive_matches" name="Finding_successive_matches">成功する一致の検索</h3> + +<p>正規表現で "<code>g</code>" フラグを使用する場合、同じ文字列で成功する一致を見つけるために <code>exec()</code> メソッドを複数回使うことができます。その際、検索は正規表現オブジェクトの {{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティで指定された位置の <code><var>str</var></code> の部分文字列から始まります ({{jsxref("RegExp.prototype.test()", "test()")}} も {{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティの位置から始めます)。なお、別な文字列を検索する場合でも {{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティはリセットされず、既存の {{jsxref("RegExp.lastIndex", "lastIndex")}} から検索を始めます。</p> + +<p>例えば、次のスクリプトを考えてみてください。</p> + +<pre class="brush: js notranslate">let myRe = /ab*/g; +let str = 'abbcdefabh'; +let myArray; +while ((myArray = myRe.exec(str)) !== null) { + let msg = myArray[0] + ' を見つけました。'; + msg += '次の検索は ' + myRe.lastIndex + ' からです。'; + console.log(msg); +} +</pre> + +<p>このスクリプトは以下のテキストを表示します。</p> + +<pre class="notranslate">abb を見つけました。次の検索は 3 からです。 +ab を見つけました。次の検索は 9 からです。 +</pre> + +<div class="warning"> +<p><strong>警告:</strong> <strong>正規表現リテラル (または {{jsxref("RegExp")}} コンストラクター) を <code>while</code> の条件の中に配置しないでください。</strong></p> + +<p><strong>{{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティが繰り返し毎にリセットされるので、無限ループになります。</strong></p> + +<p><strong>また、グローバルフラグ ("<code>g</code>") が設定されていることを確認してください。これも無限ループを引き起こします。</strong></p> +</div> + +<h3 id="Using_exec_with_RegExp_literals" name="Using_exec_with_RegExp_literals"><strong>RegExp リテラルでの exec() の使用</strong></h3> + +<p><strong>{{jsxref("RegExp")}} オブジェクトを作成せずに <code>exec()</code> を使用することもできます。</strong></p> + +<pre class="brush: js notranslate"><strong>let matches = /(hello \S+)/.exec('This is a hello world!'); +console.log(matches[1]); +</strong></pre> + +<p><strong>これで 'hello world!' を含んだメッセージをログ出力します。</strong></p> + +<h2 id="Specifications" name="Specifications"><strong>仕様書</strong></h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col"><strong>仕様書</strong></th> + </tr> + </thead> + <tbody> + <tr> + <td><strong>{{SpecName('ESDraft', '#sec-regexp.prototype.exec', 'RegExp.exec')}}</strong></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility"><strong>ブラウザーの互換性</strong></h2> + +<div> +<div class="hidden"><strong>このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</strong></div> + +<p><strong>{{Compat("javascript.builtins.RegExp.exec")}}</strong></p> +</div> + +<h2 id="See_also" name="See_also"><strong>関連情報</strong></h2> + +<ul> + <li><strong><a href="/ja/docs/Web/JavaScript/Guide">JavaScript ガイド</a> の<a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions">正規表現</a>の章</strong></li> + <li><strong>{{jsxref("RegExp")}}</strong></li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/flags/index.html b/files/ja/web/javascript/reference/global_objects/regexp/flags/index.html new file mode 100644 index 0000000000..22ce044b2d --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/flags/index.html @@ -0,0 +1,73 @@ +--- +title: RegExp.prototype.flags +slug: Web/JavaScript/Reference/Global_Objects/RegExp/flags +tags: + - ECMAScript 2015 + - JavaScript + - Property + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/flags +--- +<div>{{JSRef}}</div> + +<p><strong><code>flags</code></strong> プロパティは、現在の正規表現オブジェクトの<a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions#Advanced_searching_with_flags_2">フラグ</a>から成る文字列を返します。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-flags.html")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<p>{{JS_Property_Attributes(0, 0, 1)}}</p> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>flags</code> プロパティのフラグはアルファベット順にソートされます(左から右へ 例えば、<code>"gimuy"</code>)。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_flags" name="Using_flags">flags の使用</h3> + +<pre class="brush: js; notranslate">/foo/ig.flags; // "gi" +/bar/myu.flags; // "muy" +</pre> + +<h2 id="Polyfill" name="Polyfill">ポリフィル</h2> + +<pre class="brush: js; notranslate">if (RegExp.prototype.flags === undefined) { + Object.defineProperty(RegExp.prototype, 'flags', { + configurable: true, + get: function() { + return this.toString().match(/[gimsuy]*$/)[0]; + } + }); +} +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName("ESDraft", "#sec-get-regexp.prototype.flags", "RegExp.prototype.flags")}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.flags")}}</p> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{JSxRef("RegExp.prototype.source")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/global/index.html b/files/ja/web/javascript/reference/global_objects/regexp/global/index.html new file mode 100644 index 0000000000..53640842fe --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/global/index.html @@ -0,0 +1,80 @@ +--- +title: RegExp.prototype.global +slug: Web/JavaScript/Reference/Global_Objects/RegExp/global +tags: + - JavaScript + - Property + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/global +--- +<div>{{JSRef}}</div> + +<p><strong><code>global</code></strong> プロパティは "<code>g</code>" フラグが正規表現で使われているかどうかを返します。<code>global</code> は個々の正規表現インスタンスの読取専用プロパティです。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-global.html")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<div>{{js_property_attributes(0, 0, 1)}}</div> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>global</code> の値は {{jsxref("Boolean")}} です。 <code>true</code> は "<code>g</code>" フラグを使用していることを表します。それ以外は <code>false</code> になります。 "<code>g</code>" フラグは、その正規表現が文字列の中で一致する可能性がある場所すべてについてテストを行うことを示します。 <code>global</code> ("<code>g</code>") と <code>sticky</code> ("<code>y</code>") の両方が指定された正規表現では、 <code>global</code> フラグが無視され、粘着的な比較が行われます。</p> + +<p>このプロパティを直接変更することはできません。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_global" name="Using_global">global の使用</h3> + +<pre class="brush: js notranslate">var regex = new RegExp('foo', 'g'); + +console.log(regex.global); // true + +var str = 'fooexamplefoo'; + +var str1 = str.replace(regex, ''); + +console.log(str1); // Output: example + +var regex1 = new RegExp('foo'); + +var str2 = str.replace(regex1, ''); + +console.log(str2); // Output: examplefoo</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.global', 'RegExp.prototype.global')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.global")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.lastIndex")}}</li> + <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li> + <li>{{jsxref("RegExp.prototype.multiline")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> + <li>{{jsxref("RegExp.prototype.sticky")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/ignorecase/index.html b/files/ja/web/javascript/reference/global_objects/regexp/ignorecase/index.html new file mode 100644 index 0000000000..f348af5116 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/ignorecase/index.html @@ -0,0 +1,69 @@ +--- +title: RegExp.prototype.ignoreCase +slug: Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase +tags: + - JavaScript + - Property + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase +--- +<div>{{JSRef}}</div> + +<p><strong><code>ignoreCase</code></strong> プロパティは、"<code>i</code>" フラグが正規表現で使われているかどうかを示します。<code>ignoreCase</code> は、正規表現インスタンスごとの読み取り専用プロパティです。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-ignorecase.html")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<div>{{js_property_attributes(0, 0, 1)}}</div> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>ignoreCase</code> の値は {{jsxref("Boolean")}} で、"<code>i</code>" フラグが使われていたならば、真であり、そうでなければ、偽です。"<code>i</code>" フラグは、文字列でのマッチを適用する際に、大文字と小文字の違いは無視されるべきであることを示します。</p> + +<p>このプロパティを直接変更することはできません。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_ignoreCase" name="Using_ignoreCase">ignoreCase の使用</h3> + +<pre class="brush: js notranslate">var regex = new RegExp('foo', 'i'); + +console.log(regex.ignoreCase); // true +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.ignorecase', 'RegExp.prototype.ignoreCase')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.ignoreCase")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.lastIndex")}}</li> + <li>{{jsxref("RegExp.prototype.global")}}</li> + <li>{{jsxref("RegExp.prototype.multiline")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> + <li>{{jsxref("RegExp.prototype.sticky")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/index.html b/files/ja/web/javascript/reference/global_objects/regexp/index.html new file mode 100644 index 0000000000..5fb7a2c48c --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/index.html @@ -0,0 +1,261 @@ +--- +title: RegExp +slug: Web/JavaScript/Reference/Global_Objects/RegExp +tags: + - Constructor + - JavaScript + - Reference + - RegExp + - Regular Expressions + - コンストラクター + - 正規表現 +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp +--- +<div>{{JSRef}}</div> + +<p><strong><code>RegExp</code></strong> オブジェクトは、パターンでテキストを検索するために使用します。</p> + +<p>正規表現を詳しく知りたい方は <a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions">JavaScript ガイド</a> の <a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions">正規表現</a> を参考にしてください。</p> + +<h2 id="Description" name="Description">解説</h2> + +<h3 id="Literal_notation_and_constructor" name="Literal_notation_and_constructor">リテラル記法とコンストラクター</h3> + +<p><code>RegExp</code> オブジェクトを生成するには二通りの方法があります。<em>リテラル記法</em>と<em>コンストラクター</em>です。</p> + +<ul> + <li><strong>リテラル記法</strong>は引数をスラッシュで囲み、引用符は使用しません。</li> + <li><strong>コンストラクター関数</strong>の引数はスラッシュで囲むのではなく、引用符を使用します。</li> +</ul> + +<p>以下の三つの式は、同じ正規表現を生成します。</p> + +<pre class="brush: js notranslate">/ab+c/i +new RegExp(/ab+c/, 'i') // リテラル記法 +new RegExp('ab+c', 'i') // コンストラクター +</pre> + +<p>リテラル記法では、正規表現が評価されるときにコンパイルを行います。正規表現が変化しない場合は、リテラル記法を使用してください。例えばループ内で使用する正規表現を生成するためにリテラル記法を使用すると、反復処理のたびに正規表現を再コンパイルすることはありません。</p> + +<p><code>new RegExp('ab+c')</code> といった正規表現オブジェクトのコンストラクターは、実行時に正規表現をコンパイルします。正規表現パターンが変わることがわかっている場合や、パターンが不明でありユーザー入力など別のソースからパターンを取得する場合は、コンストラクター関数を使用してください。</p> + +<h3 id="Flags_in_constructor" name="Flags_in_constructor">コンストラクターのフラグ</h3> + +<p>ECMAScript 6 より、第 1 引数が <code>RegExp</code> で第 2 引数に <code><var>flags</var></code> を指定する場合 (<code>new RegExp(/ab+c/, 'i')</code>) に {{jsxref("TypeError")}} ("can't supply flags when constructing one RegExp from another") が発生しません。代わりに、引数を元に新たな <code>RegExp</code> が生成されます。</p> + +<p>コンストラクター関数を使用する場合は、通常の文字エスケープ規則 (文字列内に特殊文字が含まれるとき、前に <code>\</code> を付加する) が必須です。</p> + +<p>例えば、以下 2 つの構文は同等です。</p> + +<pre class="brush: js notranslate">let re = /\w+/ +let re = new RegExp('\\w+') +</pre> + +<h3 id="Perl-like_RegExp_properties" name="Perl-like_RegExp_properties">Perl 風の RegExp プロパティ</h3> + +<p>{{JSxRef("RegExp")}} のプロパティのいくつかは、長い名前と短い (Perl 風の) 名前があります。 Both names always refer to the same value. (Perl is the programming language from which JavaScript modeled its regular expressions.). See also <a href="/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#RegExp_Properties">deprecated <code>RegExp</code> properties.</a></p> + +<h2 id="Constructor" name="Constructor">コンストラクター</h2> + +<dl> + <dt>{{jsxref("RegExp/RegExp", "RegExp()")}}</dt> + <dd>新しい <code>RegExp</code> オブジェクトを生成します。</dd> +</dl> + +<h2 id="Static_properties" name="Static_properties">静的プロパティ</h2> + +<dl> + <dt>{{jsxref("RegExp.@@species", "get RegExp[@@species]")}}</dt> + <dd>派生オブジェクトを生成するために使用されるコンストラクター関数です。</dd> + <dt>{{jsxref("RegExp.lastIndex")}}</dt> + <dd>次のマッチングを開始する位置です。</dd> +</dl> + +<h2 id="Instance_properties" name="Instance_properties">インスタンスプロパティ</h2> + +<dl> + <dt>{{JSxRef("RegExp.prototype.flags")}}</dt> + <dd><code>RegExp</code> オブジェクトのフラグから成る文字列です。</dd> + <dt>{{JSxRef("RegExp.prototype.dotAll")}}</dt> + <dd><code>.</code> が改行文字にマッチするかどうか。</dd> + <dt>{{JSxRef("RegExp.prototype.global")}}</dt> + <dd>対象文字列で可能なすべてのマッチに対して正規表現をテストするか、それとも、最初のマッチに対してのみテストするどうかのフラグです。</dd> + <dt>{{JSxRef("RegExp.prototype.ignoreCase")}}</dt> + <dd>文字列でのマッチを適用する際に、大文字と小文字の違いを無視するかどうかのフラグです。</dd> + <dt>{{JSxRef("RegExp.prototype.multiline")}}</dt> + <dd>複数行に渡って文字列を検索するかどうかのフラグです。</dd> + <dt>{{JSxRef("RegExp.prototype.source")}}</dt> + <dd>パターンのテキストです。</dd> + <dt>{{JSxRef("RegExp.prototype.sticky")}}</dt> + <dd>検索が<ruby>先頭固定<rp> (</rp><rt>sticky</rt><rp>) </rp></ruby>であるかどうかのフラグです。</dd> + <dt>{{JSxRef("RegExp.prototype.unicode")}}</dt> + <dd>Unicode 機能が有効かどうかのフラグです。</dd> +</dl> + +<h2 id="Instance_methods" name="Instance_methods">インスタンスメソッド</h2> + +<dl> + <dt>{{JSxRef("RegExp.prototype.compile()")}}</dt> + <dd>スクリプトの実行中に正規表現を (再) コンパイルします。</dd> + <dt>{{JSxRef("RegExp.prototype.exec()")}}</dt> + <dd>その文字列のパラメータでのマッチのための検索を実行します。</dd> + <dt>{{JSxRef("RegExp.prototype.test()")}}</dt> + <dd>その文字列のパラメータでのマッチのためのテストをします。</dd> + <dt>{{JSxRef("RegExp.prototype.toString()")}}</dt> + <dd>特定のオブジェクトを表す文字列を返します。{{JSxRef("Object.prototype.toString()")}} メソッドを上書きします。</dd> + <dt>{{JSxRef("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</dt> + <dd>与えられた文字列とのマッチを行い、マッチ結果を返します。</dd> + <dt>{{JSxRef("RegExp.prototype.@@matchAll()", "RegExp.prototype[@@matchAll]()")}}</dt> + <dd>文字列に対して正規表現で一致したものをすべて返します。</dd> + <dt>{{JSxRef("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</dt> + <dd>与えられた文字列のマッチを新しい部分文字列で置き換えます。</dd> + <dt>{{JSxRef("RegExp.prototype.@@search()", "RegExp.prototype[@@search]()")}}</dt> + <dd>与えられた文字列でマッチを検索し、文字列で見つかったパターンのインデックスを返します。</dd> + <dt>{{JSxRef("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</dt> + <dd>文字列を部分文字列に分割し、指定された文字列を配列に分割します。</dd> +</dl> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_a_regular_expression_to_change_data_format" name="Using_a_regular_expression_to_change_data_format">正規表現を使用したデータ形式の変更</h3> + +<p>以下のスクリプトは、{{jsxref("Global_Objects/String", "String")}} インスタンスの {{jsxref("String.prototype.replace()", "replace()")}} メソッドを使用して、 <em>first last</em> 形式のフォーマットでの名前にマッチさせ、<em>last, first</em> 形式のフォーマットで出力しています。</p> + +<p>置換テキスト中で、そのスクリプトは、<code>$1</code> と <code>$2</code> を使用して、それぞれ対応する正規表現パターンでマッチする括弧がキャプチャした結果を指定しています。</p> + +<pre class="brush: js notranslate">let re = /(\w+)\s(\w+)/ +let str = 'John Smith' +let newstr = str.replace(re, '$2, $1') +console.log(newstr) +</pre> + +<p>これは、 <code>"Smith, John"</code> と表示します。</p> + +<h3 id="Using_regular_expression_to_split_lines_with_different_line_endingsends_of_lineline_breaks" name="Using_regular_expression_to_split_lines_with_different_line_endingsends_of_lineline_breaks">正規表現を使用したさまざまな行末/行の終端/改行での行の分割</h3> + +<p>既定の行末文字は、プラットフォーム (Unix、Windows など) によって異なります。この例で実行する行分割は、あらゆるプラットフォームで動作します。</p> + +<pre class="brush: js notranslate">let text = 'Some text\nAnd some more\r\nAnd yet\rThis is the end' +let lines = text.split(/\r\n|\r|\n/) +console.log(lines) // logs [ 'Some text', 'And some more', 'And yet', 'This is the end' ] +</pre> + +<p>正規表現内のパターンの順序が重要であることに注意してください。</p> + +<h3 id="Using_regular_expression_on_multiple_lines" name="Using_regular_expression_on_multiple_lines">複数行で正規表現を使用する</h3> + +<pre class="brush: js notranslate">let s = 'Please yes\nmake my day!' + +s.match(/yes.*day/); +// null + +s.match(/yes[^]*day/); +// Returns ["yes\nmake my day"] +</pre> + +<h3 id="Using_a_regular_expression_with_the_sticky_flag" name="Using_a_regular_expression_with_the_sticky_flag">sticky フラグ付きの正規表現の使用</h3> + +<p>{{JSxRef("Global_Objects/RegExp/sticky", "sticky")}} フラグは、対象文字列で {{jsxref("RegExp.prototype.lastIndex")}} からマッチングを試みることにより、正規表現の sticky マッチングを実行することを示します。</p> + +<pre class="brush: js notranslate">let str = '#foo#' +let regex = /foo/y + +regex.lastIndex = 1 +regex.test(str) // true +regex.lastIndex = 5 +regex.test(str) // false (lastIndex is taken into account with sticky flag) +regex.lastIndex // 0 (reset after match failure)</pre> + +<h3 id="The_difference_between_the_sticky_flag_and_the_global_flag" name="The_difference_between_the_sticky_flag_and_the_global_flag">sticky フラグと global フラグの違い</h3> + +<p>sticky フラグ <code>y</code> を付けると、次の一致は <code>lastIndex</code> の位置で行われるのに対し、グローバルフラグ <code>g</code> を付けると、検索は <code>lastIndex</code> の位置から始められます。</p> + +<pre class="brush: js notranslate">re = /\d/y; +while (r = re.exec("123 456")) console.log(r, "AND re.lastIndex", re.lastIndex); + +// [ '1', index: 0, input: '123 456', groups: undefined ] AND re.lastIndex 1 +// [ '2', index: 1, input: '123 456', groups: undefined ] AND re.lastIndex 2 +// [ '3', index: 2, input: '123 456', groups: undefined ] AND re.lastIndex 3 +// ... and no more match.</pre> + +<p>グローバルフラグ <code>g</code> を付けると、3桁だけでなく、6桁すべてが一致します。</p> + +<h3 id="Regular_expression_and_Unicode_characters" name="Regular_expression_and_Unicode_characters">正規表現と Unicode 文字</h3> + +<p>上の表にもある通り、<code>\w</code> や <code>\W</code> は ASCII 基本文字にのみマッチします。具体的には <code>a</code> から <code>z</code> 、<code>A</code> から <code>Z</code> 、 <code>0</code> から <code>9</code> および <code>_</code> です。</p> + +<p>キリル語やヘブライ語で使われるような非 ASCII 文字にマッチさせるには <code>\u<var>hhhh</var></code> 形式 (<code><var>hhhh</var></code> の部分は 16進表記の Unicode 値) を使ってください。</p> + +<p>この例は、文字列全体から Unicode 文字列だけを抜き出す方法を示しています。</p> + +<pre class="brush: js notranslate">let text = 'Образец text на русском языке' +let regex = /[\u0400-\u04FF]+/g + +let match = regex.exec(text) +console.log(match[0]) // logs 'Образец' +console.log(regex.lastIndex) // logs '7' + +let match2 = regex.exec(text) +console.log(match2[0]) // logs 'на' [did not log 'text'] +console.log(regex.lastIndex) // logs '15' + +// and so on +</pre> + +<p><a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes">Unicode プロパティエスケープ</a>機能は <code>\p{scx=Cyrl}</code> のように単純な表記を可能にする解決策を導入しています。スクリプト別の完全な Unicode コードブロック (範囲) を知ることができる外部リソースも、 <a href="http://kourge.net/projects/regexp-unicode-block">Regexp-Unicode-block</a> などがあります。</p> + +<h3 id="Extracting_sub-domain_name_from_URL" name="Extracting_sub-domain_name_from_URL">URL からのサブドメイン名の抽出</h3> + +<pre class="brush: js notranslate">let url = 'http://xxx.domain.com' +console.log(/[^.]+/.exec(url)[0].substr(7)) // logs 'xxx' +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp-regular-expression-objects', 'RegExp')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp")}}</p> +</div> + +<h3 id="Firefox-specific_notes" name="Firefox-specific_notes">Firefox 固有の注意事項</h3> + +<p>Firefox 34 より、量指定子を伴うキャプチャグループが動作を妨げている場合に、キャプチャグループにマッチしたテキストが空文字列ではなく <code>undefined</code> になります:</p> + +<pre class="brush: js notranslate">// Firefox 33 以前 +'x'.replace(/x(.)?/g, function(m, group) { + console.log("'group:" + group + "'"); +}); +// 'group:' + +// Firefox 34 以降 +'x'.replace(/x(.)?/g, function(m, group) { + console.log("'group:" + group + "'"); +}); +// 'group:undefined' +</pre> + +<p>ウェブの互換性のため <code>RegExp.<var>$N</var></code> は引き続き、 <code>undefined</code> ではなく空文字列を返します (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1053944">bug 1053944</a>)。</p> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li><a href="/ja/docs/Web/JavaScript/Guide">JavaScript ガイド</a>内の<a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions">正規表現</a>の章</li> + <li>{{jsxref("String.prototype.match()")}}</li> + <li>{{jsxref("String.prototype.replace()")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/input/index.html b/files/ja/web/javascript/reference/global_objects/regexp/input/index.html new file mode 100644 index 0000000000..5059ed7598 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/input/index.html @@ -0,0 +1,67 @@ +--- +title: RegExp.input ($_) +slug: Web/JavaScript/Reference/Global_Objects/RegExp/input +tags: + - JavaScript + - Non-standard + - Property + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/input +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>標準外の <strong><code>input</code></strong> プロパティは正規表現の一致している文字列を含む静的プロパティです。 <code>RegExp.$_</code> はこのプロパティのエイリアスです。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>input</code> プロパティは静的プロパティです。個々の正規表現オブジェクトのプロパティではありません。常に、 <code>RegExp.input</code> または <code>RegExp.$_.</code> として使用してください。</p> + +<p><code><strong>input</strong></code> プロパティの値は、正規表現の検索文字列が変更され文字列が一致しているときはいつでも変更されます。</p> + +<h2 id="Examples">Examples</h2> + +<h3 id="Using_lastIndex" name="Using_lastIndex">input と $_ の使用</h3> + +<pre class="brush: js notranslate">var re = /hi/g; +re.test('hi there!'); +RegExp.input; // "hi there!" +re.test('foo'); // new test, non-matching +RegExp.$_; // "hi there!" +re.test('hi world!'); // new test, matching +RegExp.$_; // "hi world!" +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('Legacy RegExp features')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.lastMatch")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}}</li> + <li>{{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}}</li> + <li>{{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}}</li> + <li>{{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}}</li> + <li>{{jsxref("RegExp.n", "RegExp.$1-$9")}}</li> +</ul> 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 +--- +<div>{{JSRef}}</div> + +<p><strong><code>lastIndex</code></strong> は正規表現インスタンスの読み書き可能なプロパティで、次の一致を開始する位置を指定します。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-lastindex.html")}}</div> + +<div>{{js_property_attributes(1, 0, 0)}}</div> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox"><code><var>regExpObj</var>.lastIndex</code></pre> + +<h2 id="Description" name="Description">解説</h2> + +<p>このプロパティは、正規表現インスタンスがグローバル検索を示すために <code>g</code> フラグを使用した場合、または粘着的検索を示すために <code>y</code> フラグを使用した場合にのみ設定されます。以下の規則が適用されます。</p> + +<ul> + <li><code>lastIndex</code> が文字列の長さよりも大きければ、 {{jsxref("RegExp.prototype.test()", "test()")}} および {{jsxref("RegExp.prototype.exec()", "exec()")}} は失敗し、<code>lastIndex</code> は 0 にセットされます。</li> + <li><code>lastIndex</code> が文字列の長さ以下で、かつ正規表現が空文字列に一致する場合には、正規表現は <code>lastIndex</code> から始まる入力に一致します。</li> + <li><code>lastIndex</code> が文字列の長さと等しく、かつ、正規表現が空文字列に一致しない場合、正規表現は入力に一致せず、 <code>lastIndex</code> は 0 にリセットされます。</li> + <li>それ以外の場合は、 <code>lastIndex</code> は直近の一致に続く次の位置に設定されます。</li> +</ul> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="lastIndex_の使用">lastIndex の使用</h3> + +<p>例えば、以下の連続した処理を考えてみてください。:</p> + +<pre class="brush: js">var re = /(hi)?/g; +</pre> + +<p>空文字列に一致します。</p> + +<pre class="brush: js">console.log(re.exec('hi')); +console.log(re.lastIndex); +</pre> + +<p><code>lastIndex</code> が 2 になり<code>["hi", "hi"]</code> が返ります。</p> + +<pre class="brush: js">console.log(re.exec('hi')); +console.log(re.lastIndex); +</pre> + +<p>ゼロ番目の要素が一致した文字列なので、 <code>["", undefined]</code> という空配列が返ります。この場合、 <code>lastIndex</code> が 2 であったときに (そして 2 のままである)、 <code>hi</code> の長さが 2 であるので、空文字列になります。</p> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-properties-of-regexp-instances', 'RegExp.lastIndex')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.lastIndex")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li> + <li>{{jsxref("RegExp.prototype.global")}}</li> + <li>{{jsxref("RegExp.prototype.multiline")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> + <li>{{jsxref("RegExp.prototype.sticky")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/lastmatch/index.html b/files/ja/web/javascript/reference/global_objects/regexp/lastmatch/index.html new file mode 100644 index 0000000000..90b918a125 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/lastmatch/index.html @@ -0,0 +1,67 @@ +--- +title: RegExp.lastMatch ($&) +slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch +tags: + - JavaScript + - Non-standard + - Property + - Read-only + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>標準外の <strong>lastMatch</strong> プロパティは、最後に一致した文字列を含む正規表現の静的で読み取り専用のプロパティです。 <code>RegExp.$&</code> はこのプロパティに対するエイリアスです。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>lastMatch</code> プロパティは静的プロパティです。個々の正規表現オブジェクトのプロパティではありません。そのかわりに、常に、<code>RegExp.lastMatch</code>または<code>RegExp['$&'].</code>として使用してください。</p> + +<p><code>lastMatch</code> プロパティの値は、読み取り専用で、一致に成功するたびに変更されます。</p> + +<p>ドットプロパティアクセサー (<code>RegExp.$&</code>) で短縮エイリアスを使用することはできません。その場合、パーサーは "&" という表現を期待して {{jsxref("SyntaxError")}} が発生します。<a href="/docs/Web/JavaScript/Reference/Operators/Property_Accessors">プロパティへのアクセスにはブラケット表記法</a>を使用してください。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_lastMatch_and" name="Using_lastMatch_and">lastMatch と $& の使用</h3> + +<pre class="brush: js notranslate">var re = /hi/g; +re.test('hi there!'); +RegExp.lastMatch; // "hi" +RegExp['$&']; // "hi" +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('Legacy RegExp features')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.lastMatch")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.input", "RegExp.input ($_)")}}</li> + <li>{{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}}</li> + <li>{{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}}</li> + <li>{{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}}</li> + <li>{{jsxref("RegExp.n", "RegExp.$1-$9")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/lastparen/index.html b/files/ja/web/javascript/reference/global_objects/regexp/lastparen/index.html new file mode 100644 index 0000000000..6785a55d39 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/lastparen/index.html @@ -0,0 +1,67 @@ +--- +title: RegExp.lastParen ($+) +slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastParen +tags: + - JavaScript + - Non-standard + - Property + - Read-only + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/lastParen +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>標準外の <strong><code>lastParen</code></strong> プロパティは静的かつ読み取り専用の正規表現のプロパティで、最後に一致した括弧で括られた部分文字列を含みます。 <code>RegExp.$+</code> はこのプロパティに対するエイリアスです。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>lastParen</code> プロパティは静的プロパティです。個々の正規表現オブジェクトのプロパティではありません。常に、 <code>RegExp.lastParen</code> または <code>RegExp['$+']</code> として使用してください。</p> + +<p><code>lastParen</code> プロパティの値は読み取り専用で、一致に成功するたびに変更されます。</p> + +<p>ドットプロパティアクセサー (<code>RegExp.$+</code>) で短縮エイリアスを使用することはできません。その場合、パーサーは "+" という表現を期待して {{jsxref("SyntaxError")}} が発生します。<a href="/docs/Web/JavaScript/Reference/Operators/Property_Accessors">プロパティへのアクセスにはブラケット表記法</a>を使用してください。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_lastMatch_and" name="Using_lastMatch_and">lastMatch と $+ の使用</h3> + +<pre class="brush: js notranslate">var re = /(hi)/g; +re.test('hi there!'); +RegExp.lastParen; // "hi" +RegExp['$+']; // "hi" +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('Legacy RegExp features')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.lastParen")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.input", "RegExp.input ($_)")}}</li> + <li>{{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}}</li> + <li>{{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}}</li> + <li>{{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}}</li> + <li>{{jsxref("RegExp.n", "RegExp.$1-$9")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/leftcontext/index.html b/files/ja/web/javascript/reference/global_objects/regexp/leftcontext/index.html new file mode 100644 index 0000000000..f97a676da6 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/leftcontext/index.html @@ -0,0 +1,67 @@ +--- +title: RegExp.leftContext ($`) +slug: Web/JavaScript/Reference/Global_Objects/RegExp/leftContext +tags: + - JavaScript + - Non-standard + - Property + - Read-only + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/leftContext +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>標準外の <strong><code>input</code></strong> プロパティは、正規表現の一致している文字列を含む静的プロパティです。 <code>RegExp.$`</code> はこのプロパティのエイリアスです。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>leftContext</code> プロパティは静的プロパティです。個々の正規表現オブジェクトのプロパティではありません。常に、 <code>RegExp.leftContext</code> または <code>RegExp['$`']</code> として使用してください。</p> + +<p><code>leftContext</code> プロパティの値は読み取り専用で、一致に成功するたびに変更されます。</p> + +<p>ドットプロパティアクセサー (<code>RegExp.$`</code>) で短縮エイリアスを使用することはできません。その場合、パーサーはテンプレート文字列の開始をを期待して {{jsxref("SyntaxError")}} が発生します。<a href="/docs/Web/JavaScript/Reference/Operators/Property_Accessors">プロパティへのアクセスにはブラケット表記法</a>を使用してください。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_lastIndex" name="Using_lastIndex">input と $` の使用</h3> + +<pre class="brush: js notranslate">var re = /world/g; +re.test('hello world!'); +RegExp.leftContext; // "hello " +RegExp['$`']; // "hello " +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('Legacy RegExp features')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.leftContext")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.input", "RegExp.input ($_)")}}</li> + <li>{{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}}</li> + <li>{{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}}</li> + <li>{{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}}</li> + <li>{{jsxref("RegExp.n", "RegExp.$1-$9")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/multiline/index.html b/files/ja/web/javascript/reference/global_objects/regexp/multiline/index.html new file mode 100644 index 0000000000..4ca4a99ed3 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/multiline/index.html @@ -0,0 +1,69 @@ +--- +title: RegExp.prototype.multiline +slug: Web/JavaScript/Reference/Global_Objects/RegExp/multiline +tags: + - JavaScript + - Property + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/multiline +--- +<div>{{JSRef}}</div> + +<p><strong><code>multiline</code></strong> プロパティは、正規表現で "<code>m</code>" フラグが使用されているかどうかを示します。<code>multiline</code> は正規表現インスタンスごとの読み取り専用プロパティです。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-multiline.html", "taller")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<div>{{js_property_attributes(0, 0, 1)}}</div> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>multiline</code> の値は {{jsxref("Boolean")}} で、"<code>m</code>" フラグが使われていたならば、真であり、そうでなければ、偽です。"<code>m</code>" フラグは複数行の入力文字列が複数行として扱われるべきであることを示します。例えば、"<code>m</code>" フラグが使われているならば、"<code>^</code>" と "<code>$</code>" は、文字列の全体の始まりと終端だけにマッチする特殊文字から、文字列内の複数行の始まりと終端だけにマッチする特殊文字に変化します。</p> + +<p>このプロパティを直接変更することはできません。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_multiline" name="Using_multiline">multiline の使用</h3> + +<pre class="brush: js notranslate">var regex = new RegExp('foo', 'm'); + +console.log(regex.multiline); // true +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.multiline', 'RegExp.prototype.multiline')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.multiline")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.lastIndex")}}</li> + <li>{{jsxref("RegExp.prototype.global")}}</li> + <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> + <li>{{jsxref("RegExp.prototype.sticky")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/n/index.html b/files/ja/web/javascript/reference/global_objects/regexp/n/index.html new file mode 100644 index 0000000000..124513909e --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/n/index.html @@ -0,0 +1,83 @@ +--- +title: RegExp.$1-$9 +slug: Web/JavaScript/Reference/Global_Objects/RegExp/n +tags: + - JavaScript + - Non-standard + - Property + - Read-only + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/n +--- +<div>{{JSRef}}</div> + +<p>旧来の <strong>$1, $2, $3, $4, $5, $6, $7, $8, $9</strong> プロパティは、正規表現の静的かつ読み取り専用プロパティで、括弧で括られた部分文字列に一致したものを含みます。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p>$1, ..., $9 プロパティは静的です。個々の正規表現オブジェクトのプロパティではありません。そのため、常に <code>RegExp.$1</code>, ..., <code>RegExp.$9</code>として使用してください。</p> + +<p>これらのプロパティの値は読み取り専用で、一致に成功するたびに変更されます。</p> + +<p>括弧で括られた部分文字列の数に制限はありませんが、 <code>RegExp</code> オブジェクトは最初の 9 つの部分文字列しか保持できません。返された配列の添字を使用すると、すべての括弧付き部分文字列にアクセスすることができます。</p> + +<p>これらのプロパティは、 {{jsxref("String.replace")}} メソッドの置換テキストで使用することができます。この方法で使用する場合は、これらのプロパティの前に RegExp を付けないでください。以下の例は、これを示しています。正規表現に括弧が含まれていない場合、スクリプトは <code>$n</code> を文字通り解釈します (ここで <code>n</code> は正の整数です)。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_n_with_String.replace" name="Using_n_with_String.replace">$n を String.replace で使用</h3> + +<p>次のスクリプトは、 {{jsxref("String")}} インスタンスの {{jsxref("String.prototype.replace()", "replace()")}} メソッドを使用して、名前を <em>first last</em> の形式で一致させ、 <em>last, first</em> の形式で出力します。置換テキストでは、スクリプトは <code>$1</code> と <code>$2</code> を使用して、正規表現パターンで対応する一致する括弧の結果を示しています。</p> + +<pre class="brush: js notranslate">var re = /(\w+)\s(\w+)/; +var str = 'John Smith'; +str.replace(re, '$2, $1'); // "Smith, John" +RegExp.$1; // "John" +RegExp.$2; // "Smith" +</pre> + +<h3 id="Using_n_with_RegExp.test" name="Using_n_with_RegExp.test"></h3> + +<p>次のスクリプトは、 {{jsxref("RegExp")}} インスタンスの {{jsxref("RegExp.prototype.test()", "test()")}} メソッドを使用して、一般的な文字列で数値を取得しています。</p> + +<pre class="brush: js notranslate">var str = 'Test 24'; +var number = /(\d+)/.test(str) ? RegExp.$1 : '0'; +number; // "24" +</pre> + +<p><code>re.test(str)</code> 呼び出しと <code>RegExp.$n</code> プロパティの間で他の正規表現を使用する操作は、副作用がある可能性があることに注意してください。そのため、これらの特殊なプロパティへのアクセスは即座に行うべきで、そうしないと予期せぬ結果になる可能性があります。</p> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('Legacy RegExp features')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.n")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.input", "RegExp.input ($_)")}}</li> + <li>{{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}}</li> + <li>{{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}}</li> + <li>{{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}}</li> + <li>{{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/regexp/index.html b/files/ja/web/javascript/reference/global_objects/regexp/regexp/index.html new file mode 100644 index 0000000000..d4af61a39d --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/regexp/index.html @@ -0,0 +1,114 @@ +--- +title: RegExp() コンストラクター +slug: Web/JavaScript/Reference/Global_Objects/RegExp/RegExp +tags: + - Constructor + - JavaScript + - Reference + - RegExp +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/RegExp +--- +<div>{{JSRef}}</div> + +<p><strong><code>RegExp</code></strong> コンストラクターは、パターンに従ったテキストを検索する正規表現オブジェクトを生成します。</p> + +<p>正規表現について詳しく知りたい方は <a href="/ja/docs/Web/JavaScript/Guide">JavaScript ガイド</a> の <a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions">正規表現</a> を参考にしてください。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-constructor.html")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<p>リテラル記法、コンストラクター記法、ファクトリ記法を使用することができます。</p> + +<pre class="syntaxbox notranslate">/<var>pattern</var>/<var>flags</var> +new RegExp(<var>pattern</var>[, <var>flags</var>]) +RegExp(<var>pattern</var>[, <var>flags</var>]) +</pre> + +<h3 id="Parameters" name="Parameters">引数</h3> + +<dl> + <dt><code><var>pattern</var></code></dt> + <dd>正規表現のテキストです。</dd> + <dd>ES5 以降では、別な <code>RegExp</code> オブジェクトまたはリテラルにすることができます (後者は 2 つの RegExp コンストラクター記法のみ)。パターンには<a href="https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Regular_Expressions#Using_special_characters">特殊文字</a>を含めることができるため、文字列リテラルよりも広い範囲の値に一致させることができます。</dd> + <dt><code><var>flags</var></code></dt> + <dd> + <p>指定された場合、 <code><var>flags</var></code> は追加するフラグを含む文字列です。</p> + + <p>または、パターン用のオブジェクトが提供された場合は <code><var>flags</var></code> の文字列は他のオブジェクトのフラグを置き換えます (そして <code>lastIndex</code> は 0 にリセットされます) (ES2015以降)。</p> + + <p><code><var>flags</var></code> が指定されず正規表現オブジェクトが提供されない場合、オブジェクトのフラグ (と <code>lastIndex</code> の値) がコピーされます。</p> + + <p><code><var>flags</var></code> は以下の値を任意の組み合わせを含んだ文字列を指定することができます。</p> + + <dl> + <dt><code>g</code> (グローバルマッチ)</dt> + <dd>最初のマッチの後に止まることなくすべての一致するものを探す。</dd> + <dt><code>i</code> (大文字・小文字の無視)</dt> + <dd><code>u</code> フラグが有効な場合は、 Unicode による大文字・小文字の扱いを行います。</dd> + <dt><code>m</code> (複数行)</dt> + <dd>先頭および終端を示す文字 (<code>^</code> と <code>$</code>) が、複数の行にまたがって機能します (すなわち、入力文字列全体の先頭および終端だけでなく、<em>各々の</em>行 (<code>\n</code> や <code>\r</code> で区切られる) の先頭および末尾に一致します)。</dd> + <dt><code>s</code> ("dotAll")</dt> + <dd><code>.</code> を改行文字に一致させることができます。</dd> + <dt><code>u</code> (unicode)</dt> + <dd>パターンを Unicode コードポイントの並びとして扱います (<a href="/ja/docs/Web/API/DOMString/Binary">バイナリ文字列</a>も参照)。</dd> + <dt><code>y</code> (<ruby>先頭固定 <rp>(</rp><rt>sticky</rt><rp>)</rp></ruby>)</dt> + <dd>対象文字列中の正規表現の <code>lastIndex</code> プロパティによって示された位置からのみ比較するようになります (それより後の位置からの比較は試みません)。</dd> + </dl> + </dd> +</dl> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Literal_notation_and_constructor" name="Literal_notation_and_constructor">リテラル記法とコンストラクター</h3> + +<p><code>RegExp</code> オブジェクトを生成するのに<em>リテラル記法</em>と<em>コンストラクター</em>の2つの方法があります。</p> + +<ul> + <li><strong>リテラル記法</strong>の引数はスラッシュの間に入れ、引用符を使用しません。</li> + <li><strong>コンストラクター関数</strong>の引数はスラッシュの間に入れるのではなく、引用符を使用します。</li> +</ul> + +<p>以下の3つの式は、同じ正規表現を生成するものです。</p> + +<pre class="brush: js notranslate">/ab+c/i +new RegExp(/ab+c/, 'i') // リテラル表記 +new RegExp('ab+c', 'i') // コンストラクター +</pre> + +<p>リテラル記法は、式が評価されるときに正規表現をコンパイルした結果となります。正規表現が変化しない場合はリテラル記法を使用してください。例えば、ループで使用される正規表現を構築するためにリテラル記法を使用した場合、正規表現は反復ごとに再コンパイルされません。</p> + +<p>正規表現オブジェクトのコンストラクター、例えば <code>new RegExp('ab+c')</code> は、正規表現の実行時にコンパイルを行います。コンストラクター関数は、正規表現のパターンが変化することがわかっている場合や、パターンがわからず、ユーザー入力などの別のソースから取得している場合に使用してください。</p> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp-constructor', 'RegExp constructor')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li><a href="/ja/docs/Web/JavaScript/Guide">JavaScript ガイド</a>内の<a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions">正規表現</a>の章</li> + <li>{{jsxref("String.prototype.match()")}}</li> + <li>{{jsxref("String.prototype.replace()")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/rightcontext/index.html b/files/ja/web/javascript/reference/global_objects/regexp/rightcontext/index.html new file mode 100644 index 0000000000..755f4d434f --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/rightcontext/index.html @@ -0,0 +1,67 @@ +--- +title: RegExp.rightContext ($') +slug: Web/JavaScript/Reference/Global_Objects/RegExp/rightContext +tags: + - JavaScript + - Non-standard + - Property + - Read-only + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/rightContext +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>標準外の <strong>rightContext</strong> プロパティは静的かつ読み取り専用の正規表現のプロパティで、最新の一致に続く部分文字列が入ります。 <code>RegExp.$`</code> はこのプロパティのエイリアスです。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>rightContext</code> プロパティは静的プロパティです。個々の正規表現オブジェクトのプロパティではありません。常に、 <code>RegExp.rightContext</code> または <code>RegExp["$'"]</code> として使用してください。</p> + +<p><code>rightContext</code> プロパティの値は読み取り専用で、一致に成功するたびに変更されます。</p> + +<p>ドットプロパティアクセサー (<code>RegExp.$'</code>) で短縮エイリアスを使用することはできません。その場合、パーサーは文字列の開始をを期待して {{jsxref("SyntaxError")}} が発生します。<a href="/ja/docs/Web/JavaScript/Reference/Operators/Property_Accessors">プロパティへのアクセスにはブラケット表記法</a>を使用してください。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_rightContext_and" name="Using_rightContext_and">rightContext と $' の使用</h3> + +<pre class="brush: js notranslate">var re = /hello/g; +re.test('hello world!'); +RegExp.rightContext; // " world!" +RegExp["$'"]; // " world!" +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('Legacy RegExp features')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.rightContext")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.input", "RegExp.input ($_)")}}</li> + <li>{{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}}</li> + <li>{{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}}</li> + <li>{{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}}</li> + <li>{{jsxref("RegExp.n", "RegExp.$1-$9")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/source/index.html b/files/ja/web/javascript/reference/global_objects/regexp/source/index.html new file mode 100644 index 0000000000..ea0b05887c --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/source/index.html @@ -0,0 +1,69 @@ +--- +title: RegExp.prototype.source +slug: Web/JavaScript/Reference/Global_Objects/RegExp/source +tags: + - JavaScript + - Property + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/source +--- +<div>{{JSRef}}</div> + +<p><strong><code>source</code></strong> プロパティは、正規表現オブジェクトのソーステキストを含む {{jsxref("String")}} を返します。これには、両端の 2 つのスラッシュやフラグは含まれません。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-source.html")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<div>{{js_property_attributes(0, 0, 1)}}</div> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_source" name="Using_source">source の使用</h3> + +<pre class="brush: js notranslate">var regex = /fooBar/ig; + +console.log(regex.source); // "fooBar", /.../ と "ig" は含まれません。 +</pre> + +<h3 id="Empty_regular_expressions_and_escaping" name="Empty_regular_expressions_and_escaping">空の正規表現とエスケープ</h3> + +<p>ECMAScript 5 から、<code>source</code> プロパティは空の正規表現に対して空文字を返さなくなりました。代わりに、<code>(?:)</code> 文字列を返します。加えて、 ("\n" のような) 行区切りはエスケープされます。</p> + +<pre class="brush: js notranslate">new RegExp().source; // "(?:)" + +new RegExp('\n').source === '\n'; // ES5 以前で、true +new RegExp('\n').source === '\\n'; // ES5 から、true +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.source', 'RegExp.prototype.source')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.source")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.prototype.flags")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/sticky/index.html b/files/ja/web/javascript/reference/global_objects/regexp/sticky/index.html new file mode 100644 index 0000000000..3102d5903c --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/sticky/index.html @@ -0,0 +1,96 @@ +--- +title: RegExp.prototype.sticky +slug: Web/JavaScript/Reference/Global_Objects/RegExp/sticky +tags: + - ECMAScript 2015 + - JavaScript + - Property + - Prototype + - Reference + - RegExp + - Regular Expressions + - プロパティ + - 正規表現 +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/sticky +--- +<div>{{JSRef}}</div> + +<p><strong><code>sticky</code></strong> プロパティは、検索が<ruby>粘着的<rp> (</rp><rt>sticky</rt><rp>) </rp></ruby>である (文字列内の検索を、この正規表現の {{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティで示されたインデックスからのみ開始する) かどうかを表します。 <code>sticky</code> は個々の正規表現オブジェクトが持つ読み取り専用のプロパティです。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-sticky.html", "taller")}}</div> + +<p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p> + +<div>{{js_property_attributes(0, 0, 1)}}</div> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>sticky</code> の値は {{jsxref("Boolean")}} で、 true ならば "<code>y</code>" フラグが使われており、 false はそれ以外を表します。 "<code>y</code>" フラグは対象文字列において、この正規表現の {{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティで示されたインデックスからのみ一致することを示しています (そしてそれ以降のインデックスから一致を調べようとはしません)。 <code>sticky</code> と <code>global</code> の両方が定義された正規表現では、 <code>global</code> フラグは無視されます。</p> + +<p>このプロパティを直接変更することはできません。これは読み取り専用です。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_a_regular_expression_with_the_sticky_flag" name="Using_a_regular_expression_with_the_sticky_flag">sticky フラグのついた正規表現の使用</h3> + +<pre class="brush: js">var str = '#foo#'; +var regex = /foo/y; + +regex.lastIndex = 1; +regex.test(str); // true +regex.lastIndex = 5; +regex.test(str); // false (lastIndex is taken into account with sticky flag) +regex.lastIndex; // 0 (reset after match failure) +</pre> + +<h3 id="Anchored_sticky_flag" name="Anchored_sticky_flag">アンカーになる sticky フラグ</h3> + +<p>Firefox の SpiderMonkey エンジンのいくつかのバージョンでは <code>^</code> 指定に関する<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=773687">バグ</a>があり、 <code>^</code> アサーションで始まり、 sticky フラグを使うことで一致しない式を許可していました。このバグは Firefox 3.6 以降 (それ以前は sticky が実装されていてもバグはありませんでした) で発生し、2015年に修正されました。 ES2015 の仕様では、おそらくこのバグのために、以下のとおり定められています。</p> + +<blockquote> +<p>パターンとともに <code>y</code> フラグが使用された場合、 ^ は常に入力の始まりにのみ一致するか、 (<code>multiline</code> が <code>true</code> の場合) 最初の行に一致します。</p> +</blockquote> + +<p>以下は正しい挙動の例です。</p> + +<pre class="brush: js">var regex = /^foo/y; +regex.lastIndex = 2; +regex.test('..foo'); // false - index 2 is not the beginning of the string + +var regex2 = /^foo/my; +regex2.lastIndex = 2; +regex2.test('..foo'); // false - index 2 is not the beginning of the string or line +regex2.lastIndex = 2; +regex2.test('.\nfoo'); // true - index 2 is the beginning of a line +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.sticky', 'RegExp.prototype.sticky')}}</td> + </tr> + </thead> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.sticky")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.lastIndex")}}</li> + <li>{{jsxref("RegExp.prototype.global")}}</li> + <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li> + <li>{{jsxref("RegExp.prototype.multiline")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/test/index.html b/files/ja/web/javascript/reference/global_objects/regexp/test/index.html new file mode 100644 index 0000000000..c87e10047e --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/test/index.html @@ -0,0 +1,130 @@ +--- +title: RegExp.prototype.test() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/test +tags: + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/test +--- +<div>{{JSRef}}</div> + +<p><strong><code>test()</code></strong> メソッドは、正規表現と指定された文字列の一致を調べるための検索を実行します。 <code>true</code> または <code>false</code> を返します。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-test.html", "taller")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox notranslate"><var>regexObj</var>.test(<var>str</var>)</pre> + +<h3 id="Parameters" name="Parameters">引数</h3> + +<dl> + <dt><code><var>str</var></code></dt> + <dd>正規表現にマッチさせる文字列。</dd> +</dl> + +<h3 id="Return_value" name="Return_value">返値</h3> + +<p>正規表現と指定した文字列 <code><var>str</var></code> の間に一致するものがあった場合は、<code>true</code>。そうでない場合は、<code>false</code>。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p>あるパターンがある文字列内で見つかるかどうか調べたいときは、 <code>test()</code> を使用してください。 <code>test()</code> は論理値を返します。これは (一致した場所のインデックス番号、または見つからない場合は <code>-1</code> を返す) {{jsxref("String.prototype.search()")}} メソッドがとは異なります。</p> + +<p>より多くの情報を得るためには (実行が遅くなりますが)、 {{jsxref("RegExp.prototype.exec()", "exec()")}} メソッドを使用してください ({{jsxref("String.prototype.match()")}} メソッドと同様)。</p> + +<p><code>exec()</code> と同様に (またはその組み合わせで)、 <code>test()</code> は同じグローバル正規表現インスタンスで複数回呼び出されると、前回の一致の先に進むことになります。</p> + + + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_test" name="Using_test">test() の使用</h3> + +<p>"<code>hello</code>" が文字列の先頭近くに含まれているかを真偽値で確認する簡単な例です。</p> + +<pre class="brush: js notranslate">const str = 'hello world!'; +const result = /^hello/.test(str); + +console.log(result); // true +</pre> + +<p>次の例では、テストの成否によってメッセージを表示します。</p> + +<pre class="brush: js notranslate">function testInput(re, str) { + let midstring; + if (re.test(str)) { + midstring = 'contains'; + } else { + midstring = 'does not contain'; + } + console.log(`${str} ${midstring} ${re.source}`); +} +</pre> + +<h3 id="Using_test_on_a_regex_with_the_global_flag" name="Using_test_on_a_regex_with_the_global_flag">グローバルフラグを持つ正規表現の test() の使用</h3> + +<p>正規表現に<a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Advanced_searching_with_flags_2">グローバルフラグ</a>が設定されている場合、 <code>test()</code> は正規表現が所有する {{jsxref("RegExp.lastIndex", "lastIndex")}} の値を加算します。 ({{jsxref("RegExp.prototype.exec()", "exec()")}} も同様に <code>lastIndex</code> プロパティの値を加算します。)</p> + +<p>その後にさらに <code>test(<var>str</var>)</code> を呼び出すと、 <code><var>str</var></code> を <code>lastIndex</code> から検索します。 <code>lastIndex</code> プロパティは <code>test()</code> が <code>true</code> を返すたびに増え続けます。</p> + +<div class="blockIndicator note"> +<p><strong>補足:</strong> <code>test()</code> が <code>true</code> を返す限り、 <code>lastIndex</code> は別な文字列をテストした場合であっても、リセット<em>されません</em>。</p> +</div> + +<p><code>test()</code> が <code>false</code> を返した場合、正規表現の <code>lastIndex</code> プロパティを呼び出すと <code>0</code> にリセットされます。</p> + +<p>次の例はその挙動を示しています。</p> + +<pre class="brush: js notranslate">const regex = /foo/g; // "global" フラグを設定 + +// regex.lastIndex は 0 です。 +regex.test('foo') // true + +// regex.lastIndex は 3 です。 +regex.test('foo') // false + +// regex.lastIndex は 0 です。 +regex.test('barfoo') // true + +// regex.lastIndex は 6 です。 +regex.test('foobar') //false + +// regex.lastIndex は 0 です。 +// (...以下略) +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype.test', 'RegExp.test')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.test")}}</p> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li><a href="/ja/docs/Web/JavaScript/Guide">JavaScript ガイド</a>の<a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions">正規表現</a></li> + <li>{{jsxref("RegExp")}}</li> + <li>{{jsxref("RegExp.prototype")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/tosource/index.html b/files/ja/web/javascript/reference/global_objects/regexp/tosource/index.html new file mode 100644 index 0000000000..b56c347fda --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/tosource/index.html @@ -0,0 +1,59 @@ +--- +title: RegExp.prototype.toSource() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/toSource +tags: + - JavaScript + - Method + - Non-standard + - Obsolete + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/toSource +--- +<div>{{JSRef}} {{obsolete_header}}</div> + +<p><strong><code>toSource()</code></strong> メソッドは、オブジェクトのソースコードを表す文字列を返します。</p> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox notranslate"><var>regexObj</var>.toSource() +</pre> + +<h3 id="Return_value" name="Return_value">返値</h3> + +<p>A string representing the source code of the given {{jsxref("RegExp")}} object.</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Native_function" name="Native_function">ネイティブ関数</h3> + +<p>組み込み {{jsxref("RegExp")}} オブジェクトでは、 <code>toSource()</code> はソースコードが存在しないことを示す以下の文字列を返します。</p> + +<pre class="brush: js notranslate">function RegExp() { + [native code] +} +</pre> + +<p>{{jsxref("RegExp")}} のインスタンスでは、 <code>toSource()</code> はソースコードを表す文字列を返します。</p> + +<p>このメソッドは、通常 JavaScript によって内部的に呼び出されるものであり、コード中で明示的に呼び出されることはありません。</p> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<p>何らかの仕様の一部ではありません。</p> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.toSource")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("Object.prototype.toSource()")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/tostring/index.html b/files/ja/web/javascript/reference/global_objects/regexp/tostring/index.html new file mode 100644 index 0000000000..9e1f96e13f --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/tostring/index.html @@ -0,0 +1,83 @@ +--- +title: RegExp.prototype.toString() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/toString +tags: + - JavaScript + - Method + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/toString +--- +<div>{{JSRef}}</div> + +<p><strong><code>toString()</code></strong> メソッドは正規表現を表す文字列を返します。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-tostring.html", "taller")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<h2 id="Syntax" name="Syntax">構文</h2> + +<pre class="syntaxbox notranslate"><var>regexObj</var>.toString();</pre> + +<h3 id="Return_value" name="Return_value">返値</h3> + +<p>呼び出し元のオブジェクトを表す文字列です。</p> + +<h2 id="Description" name="Description">解説</h2> + +<p>{{jsxref("RegExp")}} オブジェクトの <code>toString()</code> メソッドは、 jsxref("Object")}} オブジェクトのものを上書きします。つまり {{jsxref("Object.prototype.toString()")}} を継承しません。 {{jsxref("RegExp")}} オブジェクトにおける <code>toString()</code> メソッドは、その正規表現オブジェクトを表す文字列を返します。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_toString" name="Using_toString">toString() の使用</h3> + +<p>以下の例は {{jsxref("RegExp")}} オブジェクトの文字列の値を表示します。:</p> + +<pre class="brush: js notranslate">var myExp = new RegExp('a+b+c'); +console.log(myExp.toString()); // logs '/a+b+c/' + +var foo = new RegExp('bar', 'g'); +console.log(foo.toString()); // logs '/bar/g' +</pre> + +<h3 id="Empty_regular_expressions_and_escaping" name="Empty_regular_expressions_and_escaping">空の正規表現とエスケープ</h3> + +<p>ECMAScript 5 以降では、空の正規表現は "/(?:)/" 文字列を返し、"\n" などの行末記号はエスケープされます。</p> + +<pre class="brush: js notranslate">new RegExp().toString(); // "/(?:)/" + +new RegExp('\n').toString() === '/\n/'; // true, prior to ES5 +new RegExp('\n').toString() === '/\\n/'; // true, starting with ES5 +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype.tostring', 'RegExp.prototype.toString')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.toString")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("Object.prototype.toString()")}}</li> +</ul> diff --git a/files/ja/web/javascript/reference/global_objects/regexp/unicode/index.html b/files/ja/web/javascript/reference/global_objects/regexp/unicode/index.html new file mode 100644 index 0000000000..ea2848a563 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/regexp/unicode/index.html @@ -0,0 +1,71 @@ +--- +title: RegExp.prototype.unicode +slug: Web/JavaScript/Reference/Global_Objects/RegExp/unicode +tags: + - ECMAScript 2015 + - JavaScript + - Property + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/unicode +--- +<div>{{JSRef}}</div> + +<p><strong><code>unicode</code></strong> プロパティは、 "<code>u</code>" フラグが正規表現で使われているかどうかを示します。<code>unicode</code> は正規表現インスタンスごとの読み取り専用プロパティです。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-unicode.html", "taller")}}</div> + +<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> + +<div>{{js_property_attributes(0, 0, 1)}}</div> + +<h2 id="Description" name="Description">解説</h2> + +<p><code>unicode</code> の値は {{jsxref("Boolean")}} です。"<code>u</code>" が使われていたら、<code>true</code> です。さもなければ、<code>false</code> です。"<code>u</code>" フラグは様々な Unicode 関連の機能を有効にします。"u" フラグで、どんな Unicode コードポイントエスケープでもそのようなものとして解釈されます。</p> + +<p>このプロパティを直接変更することはできません。読み取り専用です。</p> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Using_ignoreCase" name="Using_ignoreCase">unicode プロパティの使用</h3> + +<pre class="brush: js notranslate">var regex = new RegExp('\u{61}', 'u'); + +console.log(regex.unicode); // true +</pre> + +<h2 id="Specifications" name="Specifications">仕様書</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.unicode', 'RegExp.prototype.unicode')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div> +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.RegExp.unicode")}}</p> +</div> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("RegExp.lastIndex")}}</li> + <li>{{jsxref("RegExp.prototype.global")}}</li> + <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li> + <li>{{jsxref("RegExp.prototype.multiline")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> + <li>{{jsxref("RegExp.prototype.sticky")}}</li> +</ul> |