The purpose of indexOf is to find the location of a word in a string
lastIndexOf is also a search for words. The difference between them is that the former starts from the string header, and the latter starts from the end of the string.
Once the specified word is found, the current position number of the word will be returned. If not found, return -1.
var str = "///test/index.aspx123/"; (("/")); //0 (("/")); //39
Parameter 1 is the word to be searched for, it must be str, it will not work.
In addition, it also accepts the second parameter. Number type, this allows us to specify the range of searches.
var str = "///test/index.aspx123/"; (("/", 0)); //0 The default is 0(("/", )); //39 The default is
The control of the two methods is in different directions.
Assuming that indexOf is set to 10 , then the search range is from 10 to (end of the word)
If lastIndexOf is set to 10, the search range will be from 10 to 0 (first word)
Pay attention to this.
ps: Setting it to a negative number such as -500 will cause a strange phenomenon, I don't understand = = ";
Sometimes we want to specify the nth one. Then we can achieve it through the above method.
for example :
= function (searchValue, startIndex) { var text = this; startIndex = startIndex || 1; var is_negative = startIndex < 0; var ipos = (is_negative) ? + 1 : 0 - 1; var loopTime = (startIndex); for (var i = 0; i < loopTime ; i++) { ipos = (is_negative) ? (searchValue, ipos - 1) : (searchValue, ipos + 1); if (ipos == -1) break; } return ipos; }
var str = "///test/index.aspx123/"; (("/", 3)); //20 (("/", -2)); //25 Final2Location