Thomas
Thomas

Reputation: 34188

Regex: How to capture second part of the url by regex and jQuery

so these is my url which I need to split into two part.

1) http://localhost:8800/gb/

2) http://localhost:8800/gb/About.aspx

3) http://localhost:8800/gb/product.aspx?id=1&vref=201

4) http://localhost:8800/gb/Contact.aspx

now tell me which regex I should use to split the above url. the second part will hold the url after from country code

suppose this is my url http://localhost:8800/gb/About.aspx so second part will be About.aspx

I got one regex (https?:\/\/[^\/]+)\/(.*) which split url into two part but I want to get after from country code. thanks

Upvotes: 1

Views: 62

Answers (2)

anubhava
anubhava

Reputation: 784948

You can use split on 2 digit country code with / on either side:

var url = 'http://localhost:8800/gb/About.aspx?id=2&vref=3';
var arr = url.split(/\/[a-z]{2}\//);

console.log(arr[0]); //=> http://localhost:8800
console.log(arr[1]); //=> About.aspx?id=2&vref=3

RegEx Demo

Upvotes: 1

Marco
Marco

Reputation: 23937

One possible approach would be ^(?:\/\/|[^\/]+)*\/[a-zA-Z]{2}\/. This is simply based on Get relative URL from absolute URL with the addition to allow for a 2 character country code.

Demo: https://regex101.com/r/5fpRWX/1/

Upvotes: 2

Related Questions