Reputation: 651
I need this regular expression:
(https?:\/\/(?:w{1,3}.)?[^\s]*?(?:\.[a-z]+)+)(?![^<]*?(?:<\/\w+>|\/?>))
to match this pattern http://localhost:3000 or any url that has a port number.
Link to rubular https://rubular.com/r/tkCOv181H2KJtU
Upvotes: 1
Views: 11791
Reputation: 651
To sum up the Regular expression that matches the requirements is:
https?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)?((?:\/\w+)|(?:-\w+))*\/?(?![^<]*(?:<\/\w+>|\/?>))
This expression includes the following characters in the url:
https://rubular.com/r/7BjXQP6vaA7hvM
Upvotes: 0
Reputation: 163577
There are a few things to note in the pattern.
You have to escape the dot to match it literally in this part (?:w{1,3}\.)?
If you add the dot to the character class [^\s.]*
you don't have to make it a non greedy quantifier.
You can omit the outer capturing group if you want the match only.
You could make the port part optional (?::\d+)?
to match it:
https?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)?(?![^<]*(?:<\/\w+>|\/?>))
Upvotes: 3