JavaScript: 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's a global flag g, repeated call will start from RegExp.prototype.lastIndex. [see RegExp Flags]

[see Regex Functions]

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
JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

RegExp

prototype

Syntax

misc