xxxxxxxxxx
/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/i
xxxxxxxxxx
(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})
xxxxxxxxxx
const isUrlValid = (url: string) => {
const urlMatcher =
/^[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/i;
return !!url?.match(new RegExp(urlMatcher));
}
xxxxxxxxxx
// All are 'non-capturing' regular expressions.
// General format of a URL in a RegEx:
(?:(?:https?|ftp):\/\/)?(?:www\.)?[^/\r\n]+(?:/[^\r\n]*)?
// To enforce the protocol
(?:https?|ftp):\/\/(?:www\.)?[^/\r\n]+
// To enforce the protocol and trailing slash
(?:https?|ftp):\/\/(?:www\.)?[^/\r\n]+(?:/[^\r\n]*)
// To enforce file extensions like .html or .png:
(?:(?:https?|ftp):\/\/)?(?:www\.)?[^/\r\n]+(?:/[^\r\n]*)(?:[a-zA-Z0-9]+.(?:html|png|webp|js))
// To implement in GO (https://go.dev), add an extra backslash (' \ ') to every unescaped backslash (' \ ')
// For the first format, that is:
(?:(?:https?|ftp):\\/\\/)?(?:www\\.)?[^/\r\n]+(?:/[^\r\n]*)?
// You do not need to add the extra backslash to the '\r' and '\n' because it is handled natively
xxxxxxxxxx
function isValidUrl(url) {
// Regular expression pattern to match a valid URL
const urlPattern = /^(http|https):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\u002E\u003C\u003E]*)*(#[\w\-]+)*(\?[^\s]+)*$/;
// Test if the provided URL matches the pattern
return urlPattern.test(url);
}
// Example usage and validation
const inputUrl = 'https://example.com';
console.log(isValidUrl(inputUrl)); // Output: true
const invalidUrl = 'example.com';
console.log(isValidUrl(invalidUrl)); // Output: false
xxxxxxxxxx
/^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/
xxxxxxxxxx
^(http(s):\/\/.)[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$
xxxxxxxxxx
/((?:(?:http?|ftp)[s]*:\/\/)?[a-z0-9-%\/\&=?\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?)/gi