InRegex Logo
InRegex
New

Match Valid URL in Javascript

Explanation

This regex pattern matches a valid URL. It allows for optional 'http://' or 'https://', an optional 'www.', followed by the domain name and top-level domain. It also allows for query parameters and special characters.

Javascript

Regular Expression

/^(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$/
Copy
Do you have Feedback?
Javascript

Example Usage

const regex = /^(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$/;
const url = 'https://www.example.com/page?query=123';
if (regex.test(url)) {
    console.log('Valid URL');
}
Copy

Test the expression

Similar Regular Expressions