This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.



Returns a Boolean value that indicates whether or not a pattern exists in a searched string.


rgExp.test( str )
Required. An instance of a Regular Expression object containing the regular expression pattern and applicable flags.
Required. The string on which to perform the search.


The following example illustrates the use of the test method. To use this example, pass the function a regular expression pattern and a string. The function will test for the occurrence of the regular expression pattern in the string and return a string indicating the results of that search:

function TestDemo(re, teststring)
    // Test string for existence of regular expression.
    var found = re.test(teststring)

    // Format the output.
    var s = "";
    s += "'" + teststring + "'"

    if (found)
       s += " contains ";
       s += " does not contain ";

    s += "'" + re.source + "'"


The test method checks to see if a pattern exists within a string and returns true if so, and false otherwise.

The properties of the global RegExp object are not modified by the test method.

See also

Other articles


  • Microsoft Developer Network: Article