在索引之后查找字符串的第一个索引

Alo*_*iel 43 javascript

我有一个字符串:"www.google.com.sdg.jfh.sd"

我想找到在"sdg"之后找到的第一个".s"字符串.

所以我的索引为"sdg",由:

var start_index = str.indexOf("sdg");
Run Code Online (Sandbox Code Playgroud)

现在我需要找到在"sdg"之后找到的第一个".s"索引

任何帮助赞赏!

luk*_*nis 119

还有第二个参数控制搜索的起始位置:

String.prototype.indexOf(arg, startPosition);
Run Code Online (Sandbox Code Playgroud)

所以你可以做到

str.indexOf('s', start_index);
Run Code Online (Sandbox Code Playgroud)


mat*_*wka 20

此代码可能会有所帮助

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

你可以在这里测试一下