RegExp test() method

Description

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

Example

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


var text = "000-00-0000";
var pattern = /\d{3}-\d{2}-\d{4}/;
// ww w . j  a  va2s  .c o m
if (pattern.test(text)){
    console.log("The pattern was matched.");
}

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions