Javascript - RegExp test() Match


test() accepts a string argument and returns true if the pattern matches the argument and false if it does not.

The test() method is often used in if statements, such as the following:


var text = "123-00-0000";
var pattern = /\d{3}-\d{2}-\d{4}/;

if (pattern.test(text)){
    console.log("The pattern was matched.");


In this example, the regular expression tests for a specific numeric sequence.

If the input text matches the pattern, then a message is displayed.