Link to home
Start Free TrialLog in
Avatar of kooolsam
kooolsamFlag for United States of America

asked on

Regular Expression for URL and Port

My regular expression for a valid URL with Port number is not working.  I need it to match:

www.test1.com:8080
https://www.test2.com:80
121.1.10.1:8080

thanks
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Are those just examples, or are they the complete set of values you want to match?
Try this, assuming delimiters of / and pattern modifier flags of g and m:
/(https?:\/\/)?((www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@%_\+.~#?&\/=]*)|(\d{1,3}\.){3}\d{1,3}):\d+/gm

Open in new window


Let me know if there's a particular language or tool you're using it with.

Demo: https://regex101.com/r/fzDxc8/1
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.