pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

odule">

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

RegExp.prototype.test()

Baseline 広く利用可能

この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2015年7月以降、すべてのブラウザーで利用可能です。

test()RegExp インスタンスのメソッドで、正規表現と指定された文字列を照合するための検索を実行します。一致があった場合は true を、それ以外の場合は false を返します。

JavaScript の RegExp オブジェクトは global または sticky フラグ(/foo/g/foo/y など)を設定するとステートフルになります。これらは前回一致したときの lastIndex を格納します。これを内部的に使用することで、 test() を使用して文字列の複数の照合を反復処理することができます(キャプチャグループを使用)。

試してみましょう

const str = "table football";

const regex = new RegExp("foo*");
const globalRegex = new RegExp("foo*", "g");

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

console.log(globalRegex.lastIndex);
// 予想される結果: 0

console.log(globalRegex.test(str));
// 予想される結果: true

console.log(globalRegex.lastIndex);
// 予想される結果: 9

console.log(globalRegex.test(str));
// 予想される結果: false

構文

js
test(str)

引数

str

正規表現と照合する文字列。すべての値は文字列に変換されますので、これを省略したり undefined を渡したりすると test() は文字列 "undefined" を検索するようになります。

返値

正規表現と指定した文字列 str の間に一致するものがあった場合は、true。そうでない場合は、false

解説

あるパターンがある文字列内で見つかるかどうか調べたいときは、 test() を使用してください。 test() は論理値を返します。これは (一致した場所のインデックス番号、または見つからない場合は -1 を返す) String.prototype.search() メソッドとは異なります。

より多くの情報を得るためには (実行が遅くなりますが)、 exec() メソッドを使用してください (String.prototype.match() メソッドと同様)。

exec() と同様に (またはその組み合わせで)、 test() は同じグローバル正規表現インスタンスで複数回呼び出されると、前回の一致の先に進むことになります。

test() の使用

"hello" が文字列の先頭近くに含まれているかを論理値で確認する簡単な例です。

js
const str = "hello world!";
const result = /^hello/.test(str);

console.log(result); // true

次の例では、テストの成否によってメッセージを表示します。

js
function testInput(re, str) {
  const midString = re.test(str) ? "contains" : "does not contain";
  console.log(`${str} ${midString} ${re.source}`);
}

グローバルフラグを持つ正規表現の test() の使用

正規表現にグローバルフラグが設定されている場合、 test() は正規表現が所有する lastIndex の値を加算します。(RegExp.prototype.exec() も同様に lastIndex プロパティの値を加算します。)

その後にさらに test(str) を呼び出すと、 strlastIndex から検索します。 lastIndex プロパティは test()true を返すたびに増え続けます。

メモ: test()true を返す限り、 lastIndex は別な文字列をテストした場合であっても、リセットされません

test()false を返した場合、正規表現の lastIndex プロパティを呼び出すと 0 にリセットされます。

次の例はその挙動を示しています。

js
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 です。
regex.test("foobarfoo"); // true

// regex.lastIndex は 3 です。
regex.test("foobarfoo"); // true

// regex.lastIndex は 9 です。
regex.test("foobarfoo"); // false

// regex.lastIndex は 0 です。
// (...以下略)

仕様書

仕様書
ECMAScript® 2027 Language Specification
# sec-regexp.prototype.test

ブラウザーの互換性

関連情報

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy