Given a URL, the task is to validate it. Here we are going to declare a URL valid or Invalid by matching it with the RegExp by using JavaScript. We’re going to discuss a few methods.
Example 1: This example validates the URL = ‘https://www.geeksforgeeks.org’ by using Regular Expression.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Regular expression to match a URL. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi; var regex = new RegExp(expression); var url = 'www.geeksforgeeks.org'; el_up.innerHTML = "URL = '" + url + "'"; function gfg_Run() { var res = ""; if (url.match(regex)) { res = "Valid URL"; } else { res = "Invalid URL"; } el_down.innerHTML = res; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example validates the URL = ‘https://www.geeksforgeeksorg'(Which is wrong) by using different Regular Expression than previous one.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Regular expression to match a URL. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var expression = /(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,})/gi; var regex = new RegExp(expression); var url = 'www.geekforgeeksorg'; el_up.innerHTML = "URL = '" + url + "'"; function gfg_Run() { var res = ""; if (url.match(regex)) { res = "Valid URL"; } else { res = "Invalid URL"; } el_down.innerHTML = res; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- Convert relative path URL to absolute path URL using JavaScript
- How to check for IP address using regular expression in javascript?
- How to detect whether a device is iOS without using Regular Expression in JavaScript?
- Convert user input string into regular expression using JavaScript
- JavaScript SyntaxError - Invalid regular expression flag "x"
- Validate a password using HTML and JavaScript
- How to validate email address using RegExp in JavaScript ?
- How to validate an input is alphanumeric or not using JavaScript ?
- How to get protocol, domain and port from URL using JavaScript ?
- Get the YouTube video ID from a URL using JavaScript
- How to serialize an object into a list of URL query parameters using JavaScript ?
- How to replace plain URL with link using JavaScript ?
- How to Open URL in New Tab using JavaScript ?
- How to parse URL using JavaScript ?
- How to modify URL without reloading the page using JavaScript ?
- How to get URL Parameters using JavaScript ?
- How to open URL in a new window using JavaScript ?
- How to Display Changed Browser URL Without Reloading Through alert using JavaScript ?
- How to get the file name from page URL using JavaScript ?
- How to extract the host name from URL using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


