Url Search Or Validation – Regex

This two regexes below will help you validate a user’s input to see if the data contained an URL. For uniform purpose most of the software I worked with store URL in the database as “http://” or “https://” format. Thus I assume it is the uniform way of handling URL information.

This regex will check for both http:// and https:// . The drawn back of this regex is you can put a http://1.1.1 and it would still be a valid input. Also, this regex can’t check for http://www.com. If the input is in the format of http://www.com then the input must match the following format of “http://www.www.com”. However, for anything else, the input data can be in the format of “http://example.com”.

/(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/

This below regex will check for both http and https. It can check for both http://www.com or https://www.com. It can also check for www.abc.co.ab. The regex below does not accept http://1.1.1 as a valid URL.

/(^(([Hh][Tt][Tt][Pp])|([Hh][Tt][Tt][Pp][Ss])){1}(:\/\/){1}([^\s\.]+\.{1}[^\s\.]+\.{1}[^\s\.]{2,}|[^\s\.]+\.{1}[^\s\.]{2,}|[^\s\.]+\.{1}[^\s\.]+\.{1}[^\s\.]{1}[^\s]{2,}){1}$)/

This post was written by Kevin and was first post @ http://kevinhng86.iblog.website.
Original Post Name: "Url Search Or Validation – Regex".
Original Post Link: http://kevinhng86.iblog.website/2017/01/22/url-search-or-validation-regex/.

Advertisement


Random Article You May Like

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*