test Method

Returns a Boolean value that indicates whether a regular expression pattern exists in a searched string.

function test(str : String) : Boolean


  • str
    Required. The string on which to perform the search.


The test method checks to see if a pattern exists within a string and returns true if so, and false otherwise. If a match is found, the properties of the global RegExp object are updated to reflect the results of the match.

If the global flag is set for a regular expression, test searches the string beginning at the position indicated by the value of lastIndex. If the global flag is not set, test ignores the value of lastIndex and searches from the beginning of the string.


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 + "'"


Version 3

Applies To:

Regular Expression Object

See Also


RegExp Object


Regular Expression Syntax