remove everything before the last occurrence of a character

前端 未结 5 1321
执念已碎
执念已碎 2020-12-13 23:34

I\'m trying to perform the following action on a string :

  • find the last occurrence of the character \"/\";
  • remove everything before that
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 00:02

    You have the right idea just replace the brackets with parentheses.

    var string = "/Roland/index.php";
    var result = string.substring(string.lastIndexOf("/") + 1);
    

    Here is an example in jsfiddle and here is an explanation of the .lastIndexOf() method on the Mozilla Developer Network.

提交回复
热议问题