sona
sona

Reputation: 1552

How to get lastindex of a character in jquery?

I am getting index of a particular character in a string but not getting last index.I am using jquery function.

For example: "abch/" by index of method I am getting the index of '/',But for "aa/aaaee/" i want the index of last '/'.I have used lasytindex of but it is not working.

My code:

if (request.term.IndexOf("/") == (request.term.length - 1)) {
                        var term = request.term.slice(0, -1);
}

Upvotes: 1

Views: 234

Answers (3)

aemie
aemie

Reputation: 161

Following line return you last index

var lastindex = str.length-1;

Upvotes: 1

Kazekage Gaara
Kazekage Gaara

Reputation: 15052

Use lastIndexOf() method.

request.term.lastIndexOf("/")

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82231

You can use .lastIndexOf():

request.term.lastIndexOf("/");

Demo

Upvotes: 1

Related Questions