RegEx for selecting the first element of a path -


my question pretty simple, write regex this:

/ -> /

/foo -> /foo

/foo/bar -> /foo

/foo/bar/baz -> /foo

i've tried this:

replace(/(\/[^\/]*)\/[^\/]*/, '$1')

but returns path, unmodified.

does know how do?

this should work:

/^\/[^\/]*/ 

it match start slash , chars until slash found (excepting 2nd slash)

'/test'.match(/^\/[^\/]+/)[0] 

will return '/test'

'/test/asd'.match(/^\/[^\/]+/)[0] 

will return '/test'

strings don't start slash not match.


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -