Regular Expressions 101

Community Patterns

YouTube Video ID + URL validator

0

Regular Expression
PCRE (PHP <7.3)

/
(?:[hH][tT]{2}[pP][sS]{0,1}:\/\/)?[wW]{0,3}\.{0,1}[yY][oO][uU][tT][uU](?:\.[bB][eE]|[bB][eE]\.[cC][oO][mM])?\/(?:(?:[wW][aA][tT][cC][hH])?(?:\/)?\?(?:.*)?[vV]=([a-zA-Z0-9--]+).*|([A-Za-z0-9--]+))
/
g

Description

Verifies YouTube video links AND pulls the video ID from them in a matching group. Works with all capitalizations. It also matches the WHOLE link but sorts the video ID into a group.

Works with JavaScript, PHP and Python. (Not tested with any other Regex engine)

WARNING: youtu.be links have their ID group as the second group.

Submitted by Billy Venner - 9 years ago