search Method (String) (JavaScript)

Finds the first substring match in a regular expression search.

stringObj.search(rgExp) 

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.

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.

You can also set the i flag that causes the search to be case-insensitive.

The following example illustrates the use of the search method.

var src = "is but a Dream within a dream";
var re = /dream/;
var pos = src.search(re);
document.write(pos);
document.write("<br/>");

re = /dream/i;
pos = src.search(re);
document.write(pos);

// Output: 
// 24 
// 9

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Applies To: String Object (JavaScript)

Show:
© 2014 Microsoft