contents   index   previous   next

String search()



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



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



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.



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



var str = "one two three four five";

var pat = /th/;;     // == 8, start of th in three;     // == 4, start of t in two; // == 14, start of four


String slice()