【问题标题】:Javascript - Get index of penultimate occurrence of a string? [duplicate]Javascript - 获取字符串倒数第二次出现的索引? [复制]
【发布时间】:2019-10-18 08:52:46
【问题描述】:

我有这个字符串:

IESA1V1Ent/Cuerpo/Asiento:1/DatosRepercusion/REP_NUMDocumentoIdent/

如何获取倒数第二个/的索引?

IESA1V1Ent/Cuerpo/Asiento:1/DatosRepercusion Index Of->/ REP_NUMDocumentoIdent/

谢谢!

【问题讨论】:

  • 你尝试过什么吗?

标签: javascript string lastindexof


【解决方案1】:

您可以通过将最后一个索引作为搜索值来获取倒数第二个索引。

var string = 'IESA1V1Ent/Cuerpo/Asiento:1/DatosRepercusion/REP_NUMDocumentoIdent/',
    index = string.lastIndexOf('/', string.lastIndexOf('/') - 1);

console.log(index);
console.log(string.slice(index));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多