Check if string contains pattern using .test()

Download javascript eBook

Example

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.

Stats

Contributors: 5
2016-08-12
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook