javascript detect string if url code example
Example 1: Check if a JavaScript string is a URL
function isValidURL(string) {
var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
return (res !== null)
};
var testCase1 = "http://en.wikipedia.org/wiki/Procter_&_Gamble";
alert(isValidURL(testCase1));
Example 2: javascript url check
const isAbsoluteUrl = url => /^[a-z][a-z0-9+.-]*:/.test(url);
isAbsoluteUrl('https://1loc.dev');
isAbsoluteUrl('https://1loc.dev/foo/bar');
isAbsoluteUrl('1loc.dev');
isAbsoluteUrl('//1loc.dev');