获取第二个斜杠的值

Get value of second last slash

本文关键字:第二个 获取      更新时间:2023-09-26
var path = 'example.com/user/john/red'
path.substr(0, path.lastIndexOf("/"))

我得到example.com/user/john

如何得到只有example.com/user/?表示lastSecondIndexOf,但是没有这样的方法

函数.lastIndexOf接受fromIndex的第二个参数,您可以使用参数中斜杠的最后一个索引:

path.substr(0, path.lastIndexOf("/",path.lastIndexOf("/")-1));