How to get the last part of a string in JavaScript?

后端 未结 6 1379
野趣味
野趣味 2020-12-23 16:22

My url will look like this:

http://www.example.com/category/action

How can I get the word \"action\". This last part of the url (after the last forward slash

6条回答
  •  天命终不由人
    2020-12-23 16:50

    Well, the first thing I can think of is using the split function.

    string.split(separator, limit)
    

    Since everyone suggested the split function, a second way wood be this:

    var c = "http://www.example.com/category/action";
    var l = c.match(/\w+/g)
    alert(l)
    

    The regexp is just a stub to get the idea. Basically you get every words in the url.

    l = http,www,example,com,category,action

    get the last one.

提交回复
热议问题