RegExp.test

In this chapter you will learn:

  1. How to use RegExp's test() method in Javascript

RegExp's test()

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

<!DOCTYPE html><!--from  j  av  a2 s .c o  m-->
<html>
<head>
    <script type="text/javascript">
        var text = "000-00-0000"; 
        var pattern = /\d{3}-\d{2}-\d{4}/; 
        if (pattern.test(text)){ 
            document.writeln("The pattern was matched."); 
        } 
    </script>
</head>
<body>
</body>
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. How to convert RegExp to string in Javascript
Home » Javascript Tutorial » Regular Expressions
Regexp Type
RegExp constructor
RegExp Instance Properties
RegExp's exec()
RegExp.test
RegExp's toLocaleString() and toString()