Mendi Sterenfeld
Mendi Sterenfeld

Reputation: 397

Regex to match to urls

i'm looking for a regex to match between the 2 following url's:

1) https://www.youtube.com/
2) https://www.youtu.be/

and of course with queries.

1) https://www.youtube.com/watch?v=dqw4w9wgxcq&feature=youtu.be
2) http://youtu.be/dqw4w9wgxcq

and i want to check it after form submit.

the regex i have now is:

 var expression = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi;
  var regex = new RegExp(expression);

  if (!linkVal.match(regex)) {
    alert("PLease enter a valid url");
    return;
  }

Thanks in advance

Upvotes: 0

Views: 68

Answers (2)

plalx
plalx

Reputation: 43718

You shouldn't use a regex for that. Use the native URL object to parse the URLs and implement your logic on top of that.

const urls = [
    'http://youtube.com/',
    'https://www.youtu.be',
    'http://www.youtu.b',
    'invalid'
];

console.log(urls.map(url => [url, isValidUrl(url)]));

function isValidUrl(urlString) {
    let url;

    try { url = new URL(urlString) } 
    catch { return false }

    return isValidProtocol() && isValidDomain();

    function isValidProtocol() {
        return url.protocol === 'http:' || url.protocol === 'https:';
    }

    function isValidDomain() {
        const coreDomain = url.hostname.replace(/^www\./, '');

        return coreDomain === 'youtube.com' || coreDomain === 'youtu.be';
    }
}

Upvotes: 1

Joel Hager
Joel Hager

Reputation: 3440

The regex you're looking for is [(http(s)?):\/\/(www\.)?]{2,256}(youtube.[a-z]{2,6}|youtu.be)\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

There might be a couple circumstances that would fail this, but This is fairly robust in my testing. Let me know if any URLs fail and I can take a look. :)

const regex = /[(http(s)?):\/\/(www\.)?]{2,256}(youtube.[a-z]{2,6}|youtu.be)\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/;
const entries = [
'https://youtube.com',
'www.fakename.com',
'www.youtu.be/pathtoname',
'https://youtu.be/',
'http://youtabe.com/',
'https://youtube.com',
'https://www.youtube.com/watch?v=UuXWTnzacyQ',
'https://youtucan.com',
'https://youtunow.com'
];


const validUrls = entries.filter( e => e.match(regex) );
console.log(validUrls);

If you provide a sample list of youtube URLs, I can show that it works. I just don't have an API to grab a JSON of 100 or so videos to check.

Upvotes: 1

Related Questions