Reputation: 5992
I am looking for a regex to match some part of URL to redirect my pages accordingly.
I have googled it with no luck.
EX:
In this i am looking to match any word or char after the domain name, so it would be /cat1/cat2/
In this i am looking to match any word or char after the domain name, so it would be /cat1/cat2/cat3/
looking for 2 diff regex to match this kind of url. First regex that matches only 2 category and second regex that matches only 3 category.
Thanks guys in advance.
Upvotes: 0
Views: 401
Reputation: 5688
As far as I know, regexs do not support inverse matching. That means, you cannot write a regex that matches what is NOT in the regex (if I'm wrong, please someone correct me on this). That being said, you could use this:
^http:\/\/[^\/]+\/(.*)
Using grouping, you can match whatever comes after your URL domain. So, in this case, what you are looking for would be fetcheable through $1 (the group represented by the brackets at the end of the regex). Another important thing is the ^ in the beginning of the regex. This way you won't catch URL strings passed as query parameters of your URL.
Upvotes: 1
Reputation: 1570
This will work for the first case, with the desired part in the first capturing group:
^http://[^/]+(/(?:[^/]*/){2})$
Change the {2}
part to {3}
for the second case.
Upvotes: 2
Reputation: 56572
Sounds pretty simple.
First regex would be like : http://.+/([^/]+)/([^/]+)/
I'll let you guess what the second regex is.
Upvotes: 1
Reputation: 11144
you can get path name from windwo.location.pathname
var path = window.location.pathname
after getting this path
you can get path using split('/');
Upvotes: 0