【问题标题】:jQuery indexof from the second same character来自第二个相同字符的 jQuery indexof
【发布时间】:2013-08-21 14:01:49
【问题描述】:

如何从第二个字符中获取第二个相同的字符和索引?

字符串示例:hello&4&我正在搜索第二个相同的字符

我知道如何获得第一个“&”。

position = data.indexOf("&");

如何获得第二个“&”位置? 4号什么时候是动态的?

抱歉英语不好。

【问题讨论】:

标签: jquery indexof


【解决方案1】:
var data = "hello&4&I";
// offset is the one after the first.
var position = data.indexOf("&", data.indexOf("&") + 1);
console.log(position)
// returns 7

JSBin

哦,还有……这是香草 js。你不需要 JQuery。

【讨论】:

    【解决方案2】:

    你可以拿

    position = data.lastIndexOf("&");
    

    或者使用泛型函数来获取字符串在字符串中的第 n 个位置: How to get the nth occurrence in a string?

    【讨论】:

    猜你喜欢
    • 2012-05-08
    • 2014-12-18
    • 2018-12-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    相关资源
    最近更新 更多