javascriptstringlastindexof

Javascript - Get index of penultimate occurrence of a string?


I have this string:

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

How can I get the index of the penultimate /?

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

Thanks!


Solution

  • You could take the second last index by taking the last index as value for the searching.

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