From 07285e18a460e6b04da992318e30c1a6d49f01c1 Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Sun, 19 Sep 2021 10:47:10 +0900 Subject: Global_Objects/String/includes を更新 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Markdown に変換 - 2021/07/21 時点の英語版に同期 --- .../global_objects/string/includes/index.md | 126 +++++++++------------ 1 file changed, 53 insertions(+), 73 deletions(-) (limited to 'files/ja') diff --git a/files/ja/web/javascript/reference/global_objects/string/includes/index.md b/files/ja/web/javascript/reference/global_objects/string/includes/index.md index 04fd76a1cd..42fc7f77f6 100644 --- a/files/ja/web/javascript/reference/global_objects/string/includes/index.md +++ b/files/ja/web/javascript/reference/global_objects/string/includes/index.md @@ -7,65 +7,54 @@ tags: - Prototype - Reference - String + - Polyfill +browser-compat: javascript.builtins.String.includes translation_of: Web/JavaScript/Reference/Global_Objects/String/includes --- -
{{JSRef}}
+{{JSRef}} -

includes() メソッドは、1 つの文字列を別の文字列の中に見出すことができるかどうかを判断し、必要に応じて truefalse を返します。

+**`includes()`** メソッドは、1 つの文字列を別の文字列の中に見出すことができるかどうかを判断し、必要に応じて `true` か `false` を返します。 -
{{EmbedInteractiveExample("pages/js/string-includes.html", "shorter")}}
+{{EmbedInteractiveExample("pages/js/string-includes.html", "shorter")}} - +## 構文 -

構文

+```js +includes(searchString) +includes(searchString, position) +``` -
str.includes(searchString[, position])
+## 引数 -

引数

+- `searchString` + - : `str` 内で検索される文字列。 +- `position` {{optional_inline}} + - : 文字列内で `searchString` を検索し始める位置です。 (既定値は 0 です。) -
-
searchString
-
str 内で検索される文字列。
-
position {{optional_inline}}
-
文字列内で searchString を検索し始める位置。既定値は 0 です。
-
+### 返値 -

返値

+文字列が検索値を含んでいれば、**`true`**。含んでいれば、**`false`**。 -

文字列が検索値を含む場合、true。含まなければ、false

+## 解説 -

解説

+このメソッドで、ある文字列が別な文字列の中に含まれているかどうかを判断することができます。 -

このメソッドによってある文字列内に別の文字列を含んでいるかどうか判断できます。

+### 大文字・小文字の区別 -

大文字・小文字の区別

+`includes()` メソッドは大文字と小文字が区別します。例えば、次のコードでは `false` を返します。 -

includes() メソッドは大文字と小文字が区別します。例えば、次のコードでは false を返します:

+```js +'Blue Whale'.includes('blue') // false を返す +``` -
'Blue Whale'.includes('blue')  // returns false
-
+## ポリフィル -

+このメソッドは ECMAScript 2015 で追加されました。まだ、すべての JavaScript の実装で利用できるとは限りません。 -

includes()を使う

+しかしながら、このメソッドを簡単に代替できます。 -
const str = 'To be, or not to be, that is the question.'
-
-console.log(str.includes('To be'))        // true
-console.log(str.includes('question'))     // true
-console.log(str.includes('nonexistent'))  // false
-console.log(str.includes('To be', 1))     // false
-console.log(str.includes('TO BE'))        // false
-console.log(str.includes(''))             // true
-
- -

ポリフィル

- -

このメソッドは ECMAScript 2015 で追加されました。まだ、すべての JavaScript の実装で利用できるとは限りません。

- -

しかしながら、このメソッドを簡単にエミュレートできます。

- -
if (!String.prototype.includes) {
+```js
+if (!String.prototype.includes) {
   String.prototype.includes = function(search, start) {
     'use strict';
 
@@ -76,46 +65,37 @@ console.log(str.includes(''))             // true
     return this.indexOf(search, start) !== -1;
   };
 }
-
- -

仕様書

- - - - - - - - - - - - -
仕様書
{{SpecName('ESDraft', '#sec-string.prototype.includes', 'String.prototype.includes')}}
+``` -

ブラウザーの互換性

+## 例 -

{{Compat("javascript.builtins.String.includes")}}

+### `includes()` の使用 -

String.prototype.contains

+```js +const str = 'To be, or not to be, that is the question.' -

Firefox 18–39 では、このメソッドの名称は contains() でした。以下の理由により、 bug 1102219includes() に名称が変更されました。

+console.log(str.includes('To be')) // true +console.log(str.includes('question')) // true +console.log(str.includes('nonexistent')) // false +console.log(str.includes('To be', 1)) // false +console.log(str.includes('TO BE')) // false +console.log(str.includes('')) // true +``` -

報告されたところによると、 MooTools 1.2 を使用したいくつかのウェブサイトが Firefox 17 で壊れました。この版の MooTools は、 String.prototype.contains() が存在するかどうか調べ、存在しない場合は MooTools が独自の関数を追加するようになっています。

+## 仕様書 -

この関数が Firefox 17 で導入されたことで、このチェックの動作が変わり、 MooTools の String.prototype.contains() の実装に基づくコードが壊れることになりました。結果的に、 Firefox 17 では実装が無効化され、 String.prototype.contains() が利用できるようになったのは一つ後のバージョンである Firefox 18 で、 MooTools への働きかけによって MooTools バージョン 1.2.6 がリリースされてからでした。

+{{Specifications}} -

MooTools 1.3 では String.prototype.contains() を強制的に自分自身のものに強制したため、これに依存するウェブサイトは壊れません。しかし、このメソッドに対する MooTools 1.3 のシグニチャ と ECMAScript 2015 のシグニチャでは (第 2 引数に) 違いがあることに注意して下さい。後に、 MooTools 1.5 以降で ES2015 仕様に一致させるためにシグニチャを変更しました。

+## ブラウザーの互換性 -

Firefox 48 で、String.prototype.contains() は削除されました。 String.prototype.includes() だけを使用してください。

+{{Compat}} -

関連情報

+## 関連情報 - +- `String.prototype.includes` のポリフィルは [`core-js`](https://github.com/zloirock/core-js#ecmascript-string-and-regexp) で利用できます +- {{jsxref("Array.prototype.includes()")}} +- {{jsxref("TypedArray.prototype.includes()")}} +- {{jsxref("String.prototype.indexOf()")}} +- {{jsxref("String.prototype.lastIndexOf()")}} +- {{jsxref("String.prototype.startsWith()")}} +- {{jsxref("String.prototype.endsWith()")}} -- cgit v1.2.3-54-g00ecf