I have written regex to validate URL which could be either like
example.com
www.example.com
http://www.example.com
This is to support both http, https as optional and validate blank space is not included in url.
this.isValidURL = function (url) {
if (!url) return false;
const expression = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/gm;
return url.match(new RegExp(expression));
}