IndexOFの目的は、文字列内の単語の位置を見つけることです
LastIndexofは、単語の検索でもあります。それらの違いは、前者が文字列ヘッダーから始まり、後者が文字列の端から始まることです。
指定された単語が見つかると、単語の現在の位置番号が返されます。見つからない場合は、-1を返します。
var str = "//www.stooges.com.my/test/index.aspx123/";console.log(str.indexof("/")); //0console.log(str.lastindexof( "/ ")); // 39パラメーター1は検索する単語であり、STRでなければなりません、機能しません。
さらに、2番目のパラメーターも受け入れます。数値タイプ、これにより、検索の範囲を指定できます。
var str = "//www.stooges.com.my/test/index.aspx123/";console.log(str.indexof("/"、0)); // 0デフォルトは0console.log(str.lastindexof( "/"、str.length));です。 // 39デフォルトはstr.lengthです2つの方法の制御は異なる方向にあります。
インデックスが10に設定されていると仮定すると、検索範囲は10からstr.length(単語の終わり)です。
LastIndexofが10に設定されている場合、検索範囲は10から0(最初の単語)になります
これに注意してください。
PS:-500のような負の数に設定すると、奇妙な現象が発生します。私は理解していません= = ";
nthを指定したい場合があります。その後、上記の方法で達成できます。
例えば:
string.prototype.myindexof = function(seartValue、startIndex){var text = this; startIndex = startIndex || 1; var is_negative = startindex <0; var ipos =(is_negative)? text.length + 1:0-1; var looptime = math.abs(startIndex); text.lastIndexof(searchValue、ipos -1):text.indexof(searchValue、ipos + 1); if(ipos == -1)break;} ipos;} var str = "//www.stooges.com.my/test/index.aspx123/";console.log(str.myindexof("/"、3)); //20Console.log(str.myindexof( "/"、-2)); // 25最後の2番目のポジション