I am parsing a youtube URL and I want to get the video id but I'm having such a hard time
The only way I could come up with is this
href = 'https://www.youtube.com/watch?v=-UI86rRSHkg'
...
video = href.replace(/^.*?youtube\.com\/.*?v=(.+?)(?:&|$).*/i, '$1');
But I think there must be a better way to do this.
How can I get the value of a capture group in JavaScript regex?
?in^.*?youtubeis useless because the*means zero or more times, and the?means it's optional, which is redundant.?after*or+means to not be greedy, to stop at the following-up pattern. So in my case URLs likeyoutube.com...( missing schema andwww) would be valid.?it would still match cases without scheme orwww.Javascript regex return capture group value