1
var path = 'example.com/user/john/red'
path.substr(0, path.lastIndexOf("/"))

I got example.com/user/john

how to get only example.com/user/? means lastSecondIndexOf, but there's no such method.

Alicia
  • 163
  • 1
  • 3
  • 11

1 Answers1

5

The function .lastIndexOf takes a second parameter for fromIndex, and you could use the last index of the slash in the parameter:

path.substr(0, path.lastIndexOf("/",path.lastIndexOf("/")-1));
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54