Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - strip out text between first and second forward slashes

I've almost got this regex working but am having trouble with the leading forward slash - can anyone see where I'm going wrong with this? I just want to extract the first string "projects" from this example

  /projects/personal/29/56

see also here -> http://regexr.com?300av

like image 304
bsod99 Avatar asked Feb 13 '12 13:02

bsod99


1 Answers

The easiest way is to split string using forward slash

var firstString = url.split('/')[1];

and you will have first string, but if you want to extract using regext than this will do, just remember don't add global parameter in your regex.

\/([a-zA-Z0-9]{0,})

I hope this helps

like image 61
Senad Meškin Avatar answered Oct 04 '22 02:10

Senad Meškin