Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. RegExp
  6. test()

RegExp.prototype.test()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

Thetest() method ofRegExp instances executes a search with this regular expression for a match between a regular expression and a specified string. Returnstrue if there is a match;false otherwise.

JavaScriptRegExp objects arestateful when they havetheglobal orsticky flagsset (e.g.,/foo/g or/foo/y). They store alastIndex from the previous match. Using thisinternally,test() can be used to iterate over multiple matches in a stringof text (with capture groups).

Try it

const str = "table football";const regex = /fo+/;const globalRegex = /fo+/g;console.log(regex.test(str));// Expected output: trueconsole.log(globalRegex.lastIndex);// Expected output: 0console.log(globalRegex.test(str));// Expected output: trueconsole.log(globalRegex.lastIndex);// Expected output: 9console.log(globalRegex.test(str));// Expected output: false

Syntax

js
test(str)

Parameters

str

The string against which to match the regular expression. All values arecoerced to strings, so omitting it or passingundefined causestest() to search for the string"undefined", which is rarely what you want.

Return value

true if there is a match between the regular expression and the stringstr. Otherwise,false.

Description

Usetest() whenever you want to know whether a pattern is found in astring.test() returns a boolean, unlike theString.prototype.search() method (which returns the index of a match, or-1 if not found).

To get more information (but with slower execution), use theexec() method. (This is similar to theString.prototype.match() method.)

As withexec() (or in combination with it),test() calledmultiple times on the same global regular expression instance will advance past theprevious match.

Examples

Using test()

This example tests if"hello" is contained at the very beginning ofa string, returning a boolean result.

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

The following example logs a message which depends on the success of the test:

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

Using test() on a regex with the "global" flag

When a regex has theglobal flag set,test() will advance thelastIndex of the regex.(RegExp.prototype.exec() also advances thelastIndex property.)

Further calls totest(str) will resume searchingstr starting fromlastIndex. ThelastIndex property will continue to increase each timetest()returnstrue.

Note:As long astest() returnstrue,lastIndex willnot reset—even when testing a different string!

Whentest() returnsfalse, the calling regex'slastIndex property will reset to0.

The following example demonstrates this behavior:

js
const regex = /foo/g; // the "global" flag is set// regex.lastIndex is at 0regex.test("foo"); // true// regex.lastIndex is now at 3regex.test("foo"); // false// regex.lastIndex is at 0regex.test("barfoo"); // true// regex.lastIndex is at 6regex.test("foobar"); // false// regex.lastIndex is at 0regex.test("foobarfoo"); // true// regex.lastIndex is at 3regex.test("foobarfoo"); // true// regex.lastIndex is at 9regex.test("foobarfoo"); // false// regex.lastIndex is at 0// (...and so on)

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-regexp.prototype.test

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp