JS: RegExp.prototype.test

By Xah Lee. Date: . Last updated: .
regex_obj.test(string)

Return true if regex_obj match part or whole of string, else false.

If there is a regex flag g, repeated call start from RegExp.prototype.lastIndex.

console.log(/4/.test("345") === true);
console.log(/4/.test("999") === false);
// Repeated call with regex flag g.

const xtext = "012x45x78x";
const xre = /x/g;

console.log(xre.lastIndex);
// 0

console.log(xre.test(xtext));
// true

console.log(xre.lastIndex);
// 4

console.log(xre.test(xtext));
// true

console.log(xre.lastIndex);
// 7

JavaScript. Regular Expression