Regex to get first word after slash in URL

后端 未结 7 2178
孤街浪徒
孤街浪徒 2020-12-08 21:32

I need to get the first word after slash in a url in javascript, I assume using a regex would be ideal.

Here\'s an idea of what the URLs can possibly look like :

相关标签:
7条回答
  • 2020-12-08 22:17

    My regex is pretty bad, so I will improvise with a less efficient solution :P

    // The first part is to ensure you can handle both URLs with the http:// and those without
    
    x = window.location.href.split("http:\/\/")
    x = x[x.length-1];
    x = x.split("\/")[1]; //Result is in x
    
    0 讨论(0)
提交回复
热议问题