contents   index   previous   next



String search()

syntax:

string.search(pattern)

where:

pattern - a regular expression pattern to find or match in string.

 

return:

number - the starting position of the first matched portion or substring of the target string. Returns -1 if there is no match.

 

description:

This method returns a number indicating the offset within the string where the pattern matched or -1 if there was no match. The return is the same character position as returned by the simple search using String indexOf(). Both search() and indexOf() return the same character position of a match or find. The difference is that indexOf() is simple and search() is powerful.

 

The search() method ignores a "g" attribute if it is part of the regular expression pattern to be matched or found. That is, search() cannot be used for global searches in a string.

 

After a search is done, the appropriate RegExp object static properties are set.

 

see:

String match(), String replace(), RegExp exec(), Regular expression syntax, RegExp Object, RegExp object static properties

 

example:

var str = "one two three four five";

var pat = /th/;

str.search(pat);     // == 8, start of th in three

str.search(/t/);     // == 4, start of t in two

str.search(/Four/i); // == 14, start of four

 


String slice()