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

java - BeanIO write annotated class to fixedlength -

Using Java 8 lambdas/transformations to combine and flatten two Maps -

How to connect android app to App engine -