Javascript RegExp Instance Methods test()

Introduction

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

This method is useful when you want to know if a pattern is matched.

The test() method is often used in if statements:

let text = "555-12-1234";
let 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.




PreviousNext

Related