indexOf starting at the right

4

Problem

I would like when I make "asdhusaidhi asdhasuidhu".indexOf('s') it would return 17 referring to the last s of the string, for example, it start looking for the right ..

but instead it returned me 1 for the first s

found

Question?

How can I get him to start searching right?

    
asked by anonymous 01.04.2015 / 16:46

2 answers

6

You can use .lastIndexOf () that returns the last index where this string 's' was found.

"asdhusaidhi asdhasuidhu".lastIndexOf('s'); // 17

jsFiddle: link

    
01.04.2015 / 16:49
5

The function you want is lastIndexOf()

document.body.innerHTML += "asdhusaidhi asdhasuidhu".lastIndexOf('s');
    
01.04.2015 / 16:51