How to pluck the second value in a url after the first slash, in a string that could have many slashes?
I know there are countless regex questions out there, but I was unable to find one that fits my situation.
Suppose I have the following pathname:
/u/some_user/create/initial
How can I extract 'some_user' from this string?
I got pretty close with this:
const pathname = '/u/some_user/create/initial';
const result = pathname.match(/(\/u\/)(.{1,}\/)(.+)/);
console.log('result', result);This could potentially work if the string was '/u/some_user/create' -- It would return some_user/, and I could filter out the slash at the end. But if the string has more slashes, as above, then this just returns 'some_user/create/'.
How can I achieve plucking out just 'some_user'?
Comments
Post a Comment