Find first index of a string after index

I have a string: "www.google.com.sdg.jfh.sd"

I want to find the first ".s" string that is found after "sdg".

so I have the index of "sdg", by:

var start_index = str.indexOf("sdg");

now I need to find the first ".s" index that is found after "sdg"

any help appreciated!


Solution 1:

There's a second parameter which controls the starting position of search:

String.prototype.indexOf(arg, startPosition);

So you can do

str.indexOf('s', start_index);

Solution 2:

This code might be helpful

var string = "www.google.com.sdg.jfh.sd",
  preString = "sdg",
  searchString = ".s",
  preIndex = string.indexOf(preString),
  searchIndex = preIndex + string.substring(preIndex).indexOf(searchString);

You can test it HERE

Solution 3:

var str = "www.google.com.sdg.jfh.sd";
var search = "sdg";
var start_index = str.substring(str.indexOf(search) + search.length).indexOf(".s");