I need to create a regular expression for a youtube URL but only for the "watch url". How can be that done?
Some examples of the urls are such as this
https://www.youtube.com/watch?v=NL5ZuWmrisA
https://www.youtube.com/watch?v=62d2QvWAVt4
CodePudding user response:
To find them in a text you can use this pattern
\bhttp\S \.youtube\.com\/watch\?v=([A-Za-z0-9] )\b
The \b
is a word boundary
The ID is captured in group 1
CodePudding user response:
let input = "https://www.youtube.com/watch?v=NL5ZuWmrisA"
let pattern = /watch\?v=(?<id>. $)/gm
let groups = pattern.exec(input).groups
console.log(groups.id)
The regex checks if the input has 'watch' in it then captures the vid using named group.