Thursday, September 4, 2025
HomeLanguagesJavascriptHow to validate URL using regular expression in JavaScript?

How to validate URL using regular expression in JavaScript?

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 JavaScript RegExp. We’re going to discuss a few methods. 

Example 1: This example validates the URL = ‘https://www.geeksforgeeks.org’ by using Regular Expression

html




<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>


Output:

Validate URL using regular expression

Validate URL using regular expression

Example 2: This example validates the URL = ‘https://www.neveropenorg'(Which is wrong) by using different Regular Expression than previous one. 

html




<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.geekforneveropenorg';
    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>


Output:

Validate URL using regular expression

Validate URL using regular expression

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6628 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS