【问题标题】:Get value of second last slash获取倒数第二个斜线的值
【发布时间】:2023-03-03 07:25:22
【问题描述】:
var path = 'example.com/user/john/red'
path.substr(0, path.lastIndexOf("/"))

我得到了 example.com/user/john

如何仅获取 example.com/user/?表示lastSecondIndexOf,但没有这种方法。

【问题讨论】:

  • 这似乎回答了你的问题:stackoverflow.com/questions/5494691/…
  • 我想你的意思是你想要example.com/user 作为输出?
  • 您可以将 int 作为第二个参数传递给 lastIndexOf()
  • @Ramanlfc 这只是开始查找的起始位置。
  • 你可以使用var path = 'example.com/user/john/red' var path2 = path.substr(0, path.lastIndexOf("/")) var path3 = path2.substr(0, path.lastIndexOf("/"))

标签: javascript


【解决方案1】:

函数.lastIndexOffromIndex 提供第二个参数,您可以在参数中使用斜线的最后一个索引:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    相关资源
    最近更新 更多