[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test [Back]  [Original]

RegExp.prototype.test() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

RegExp.prototype.test()

Baseline

20157

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 truefalse

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) str lastIndex 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 LanguageSpecification
# sec-regexp.prototype.test


Web Proxy Viewer  |  New URL  |  Original Page