LoginSignup
4
4

More than 5 years have passed since last update.

JavaScriptのindexOfで二つ目のindexを得る

Last updated at Posted at 2016-06-15
var str = 'hoge hoge hoge'; // これの二つ目のhogeの位置が知りたい
var idx = str.indexOf('hoge', str.indexOf('hoge') + 1);
console.log(idx); // 5が得られる

indexOfの第二引数が検索を開始する位置なので、もう一個indexOfで探した結果に+1すれば次に出現するhogeの位置がわかるということです。めんどくさいです。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4