JS: RegExp.prototype.test

By Xah Lee. Date: . Last updated: .
regex.test(string)
Return true if regex match part or whole of string, else false. If there is a global flag g, repeated call will start from RegExp.prototype.lastIndex. [see RegExp Flag]
console.log(/4/.test("345") === true);
console.log(/4/.test("999") === false);

Repeated call with global flag.

const gx = /4/g;
console.log(gx.lastIndex === 0);
console.log(gx.test("345678") === true);
console.log(gx.lastIndex === 2);
console.log(gx.test("345678") === false);
console.log(gx.lastIndex === 0);

JavaScript, Regular Expression

BUY ΣJS JavaScript in Depth