Check if a string represents a valid URL in JavaScript
This post will discuss how to check if a string represents a valid URL in JavaScript.
There are several ways to check if a string is a valid URL in JavaScript. A valid URL must have a scheme, a host, and a path, and optionally other components such as a port, a query, or a fragment. Here are some possible functions we can use:
1. Using URL constructor
One way is to use the URL constructor, which is a built-in function that takes a string as a parameter and returns a new URL object if the string is a valid URL. The idea is to try creating a new URL object from a given string. If the string is a valid URL, it returns the URL object. If the string is not a valid URL, it throws a TypeError. We can use a try…catch statement to handle the error and check the validity of the URL. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Define a function that takes a string and returns true if it is a valid URL, false otherwise function isValid(string) { try { // Try to create a new URL object from the string new URL(string); // If no error is thrown, return true return true; } catch (error) { // If an error is thrown, return false return false; } } // Test the function on some examples console.log(isValid("https://www.example.com/")); // true console.log(isValid("javascript:void(0)")); // true console.log(isValid("example")); // false |
2. Using a regular expression
Another way is to use a regular expression (regex), which is a sequence of characters that defines a search pattern. We can use a regex to test if a string matches the pattern of a valid URL. However, a regular expression can be very complex and may not cover all possible cases of valid or invalid URLs. However, it can be useful for simple validation or filtering purposes. Here’s an example of how we can use a regex to validate a URL:
|
1 2 3 4 5 6 7 |
// Define a regular expression that matches common URL formats const urlRegex = /^(https?:\/\/)?(www\.)?([a-z0-9-]+)\.([a-z]{2,})(\/\S*)?$/i; // Test the regex on some examples console.log(urlRegex.test("https://www.example.com/")); // true console.log(urlRegex.test("javascript:void(0)")); // false console.log(urlRegex.test("example")); // false |
3. Using third-party library
A third way is to use an external library or package, such as validator.js, which is a collection of validators and sanitizers for strings and other types of data. We can use the isURL() function from this library to check if a string is a valid URL. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 |
const validator = require('validator'); // Test the library on some examples console.log(validator.isURL("https://www.example.com/")); // true console.log(validator.isURL("javascript:void(0)")); // false console.log(validator.isURL("example")); // false |
That’s all about checking if a string represents a valid URL in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)