InRegex Logo
InRegex
Nuovo

Match Valid URL in Javascript

Spiegazione

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

Espressione Regolare

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

Esempio di Utilizzo

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');
}
Copia

Prova l'espressione

Espressioni Regolari Simili