2

Sorry, for that (but I´m a regex noob)

in js how would one replace the first folder in a url string.

Pseudo Code:

Replace at start of string: /any-amount-of-characters/ with nothing

Examples

$path = '/xyz/abc/';

should result in

$path = 'abc/';

Thanks!

2 Answers 2

4
^\/[^/]*\/

Try this.Replace by empty string.See demo.

https://regex101.com/r/sH8aR8/15

var re = /^\/[^\/]*\//g;
var str = '/xyz/abc/';
var subst = '';

var result = str.replace(re, subst);
Sign up to request clarification or add additional context in comments.

1 Comment

create! Works! Thank you!
2

You need to use match the start of the string using ^:

$path = '/xyz/abc/';
$path = $path.replace(/^\/?[^\/]*\//, '');

Broken down:

  • ^ - start of string
  • \/? - optional leading / (remove the ? if required)
  • [^\/]* - any number of characters except / (use + instead of * if you there must be at least 1 character)
  • \/ trailing /

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.