Check if string contains pattern using .test
suggest changeconst re = /[a-z]+/;
if (re.test("foo")) {
console.log("Match exists.");
}
test reports whether the pattern matches. [a-z]+ matches one or more lowercase ASCII letters, so this logs Match exists. A global regular expression remembers lastIndex, so repeated test calls on the same regexp can skip ahead or return false until lastIndex wraps.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents