Check if string contains pattern using .test()

Download javascript eBook


var re = /[a-z]+/;
if (re.test("foo")) {
    console.log("Match exists.");

The test method performs a search to see if a regular expression matches a string. The regular expression [a-z]+ will search for one or more lowercase letters. Since the pattern matches the string, “match exists” will be logged to the console.


Contributors: 5
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial:

Download eBook