search Method (Windows Scripting - JScript)

 

Returns the position of the first substring match in a regular expression search.

Syntax

stringObj.search(rgExp) 

Arguments

  • stringObj
    Required. The String object or string literal on which to perform the search.

  • rgExp
    Required. An instance of a Regular Expression object containing the regular expression pattern and applicable flags.

Remarks

If a match is found, the search method returns an integer value that indicates the offset from the beginning of the string where the first match occurred. If no match is found, it returns -1.

The following example illustrates the use of the search method.

function SearchDemo()
{
    var src = "is but a dream within a dream";
    // Create regular expression pattern.
    var re = /dream/i;
    // Search the string.
    var pos = src.search(re);
    // Return the integer result.
    return (pos);

    // Output: 9
}

Requirements

Version 3

Applies To: String Object (Windows Scripting - JScript)

Change History

Date

History

Reason

March 2009

Modified example and remarks.

Information enhancement.

See Also

exec Method (Windows Scripting - JScript)
match Method (Windows Scripting - JScript)
Regular Expression Object (Windows Scripting - JScript)
Regular Expression Syntax (Scripting)
replace Method (Windows Scripting - JScript)
test Method (Windows Scripting - JScript)
Regular Expression Programming (Scripting)