Regex to get first word after slash in URL

后端 未结 7 2179
孤街浪徒
孤街浪徒 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:06

    $url = 'http://mysite.com/section/subsection';
    
    $path = parse_url($url, PHP_URL_PATH);
    
    $components = explode('/', $path);
    
    $first_part = $components[0];
    

提交回复
热议问题