| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test | [Back] [Original] |
Get to know MDN better
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
test(str)
str truefalse
test() test() ( -1 ) String.prototype.search()
() exec() (String.prototype.match() )
exec() () test()
"hello"
const str = "hello world!";
const result = /^hello/.test(str);
console.log(result); // true
function testInput(re, str) {
const midString = re.test(str) ? "contains" : "does not contain";
console.log(`${str} ${midString} ${re.source}`);
}
test() lastIndex RegExp.prototype.exec() lastIndex
test(str) str lastIndex lastIndex test() true
:
test() true lastIndex
test() false lastIndex 0
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 |