Reputation: 1217
I am trying to match below path with a regex.
# test-site could be anything like "shoe-site", "best-clock", etc.
/content/test-site
The current regex rule (\/content\/{1}\S+)
would have been adequate.
The problem is it is matching the entire path such as /content/test-site/en_US/abc.html
.
I need it to match only /content/test-site
.
Example of path to be matched:
https://localhost:4502/content/test-site/en_US/book/city/sample-rooms/airport-inn/propertyCode.00000.html
Regex I've tried by far;
(\/content\/[a-z-]+)\/[a-z]{2}_[A-Z]{2}\/book(ing-path)?\/(sample-|sample-details)(.*).[0-9]{5}.*
/content/test-site is optional- it might not present sometimes in url.
What am I doing wrong and how can I fix it?
Upvotes: 1
Views: 1325
Reputation: 37755
You can use negated character class
^(?:[^\/]*\/){2}[^\/]*
const path = '/content/test-site/en_US/abc.html';
const desired = path.match(/^(?:[^\/]*\/){2}[^\/]*/)
console.log(desired[0])
Upvotes: 1
Reputation: 4714
Regex to match any character expect /
:
content\/[^\/]+
This is character classes. A character class beginning with caret will match anything not in the class. More about this.
So, with javascript:
const url = '/content/test-site/en_US/abc.html';
const path = url.match(/content\/[^\/]+/)
console.log(path[0])
Upvotes: 1
Reputation: 5859
Here is another approach:
(?:\/[a-zA-Z0-9-]+){2}
Explanation:
(?: # Non-capturing group
\/[a-zA-Z0-9-]+ # Match starting with / and followed with characters/digits/-
) # Close grouping
{2} # Match two times, i.e. only match with two /
Upvotes: 1