URL .canParse
a modern URL checker
No
17
115
No
If you want to validate a URL in JavaScript, chances are you’re using a regular expression, using a library or a try/catch
block:
Old version
try {
new URL("https://webfromthefuture.com");
} catch (e) {
console.log("invalid URL");
}
There’s a better way, using the URL.canParse()
method. A static method that returns a boolean telling whether the URL is valid:
Future version
URL.canParse("https://webfromthefuture.com"); // true
URL.canParse("/not-a-valid-url"); // false
URL.canParse("/features/url-canparse", "https://localhost:3000"); // true
If you want to try it out and your browser supports it check out the demo below.