JavaScript Check if string contains pattern using .test()

Download JavaScript for free

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.

Creating a RegExp Object