Alex
Alex

Reputation: 6645

Regex for multiple URLs

I'm trying to create a regex that will match the following:

http://myurl.com/en/something

I need it to match the http://myrurl.com part exactly and then and 2 character code for the "en" and then anything after that. Whats the bets regex for this?

Upvotes: 2

Views: 910

Answers (3)

David
David

Reputation: 8640

This regex can match many parts of a URL

/^((http[s]?):\/\/)?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*?)?(#[\w\-]+)?$/im 

example = https://www.google.com/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash

Resulting in matches:

1: (https://) - protocol

2: (https) - protocol

3: (www.google.com) - host

4: (/dir/1/2/) - path

5: ([]) - segments of path array

6: (search.html) - file

7: (?arg=0-a&arg1=1-b&arg3-c) - query

8: (#hash) - hash

Upvotes: 0

Aram Kocharyan
Aram Kocharyan

Reputation: 20421

Try this:

(https?://[\w\d-]+\.[\w]+)/([\w\d-]+)/(.*)

Upvotes: 0

Alex K.
Alex K.

Reputation: 175758

How about;

var m = s.match(/(http:\/\/myurl.com\/)([a-z]{2})(\/.*)/i);

if (m) {
   print (m[1]);
   print (m[2]);
   print (m[3]);
}

>>http://myurl.com/
>>en
>>/something

Upvotes: 3

Related Questions