1

I have a PHP regular expression I'm using to get the YouTube video code out of a URL.

I'd love to match this with a client-side regular expression in JavaScript. Can anyone tell me how to convert the following PHP regex to JavaScript?

preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=embed/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\‌​n]+#", $url, $matches);

Much appreciated, thanks!

Chris Nolet
  • 8,714
  • 7
  • 67
  • 92
  • 1
    A straight conversion isn't possible because JavaScript doesn't support look-behind assertions. However, there are a bazillion questions on how to parse YouTube URLs with JS on Stack Overflow already. I've marked this as a possible duplicate of [Javascript regular expression for extracting Youtube video ids](http://stackoverflow.com/questions/5714739/javascript-regular-expression-for-extracting-youtube-video-ids). – Andy E Feb 16 '12 at 10:17
  • Thanks @AndyE. I'm currently using one of these JavaScript regexs, but I want mimic the PHP one exactly, since the JavaScript version is going to 'preview' what the user should see once it goes through the PHP. For this reason, I need them to fail in exactly the same way, if they do fail. – Chris Nolet Feb 16 '12 at 10:42
  • 2
    In that case why don't you re-write the server side regex so that it does not use look ahead/behind assertions? – Salman A Feb 16 '12 at 10:47
  • Final solution was to use this for both: `/.*(?:youtu.be\/|v\/|e\/|u\/\w\/|embed\/|\?v=|\&v=)([^#\&\?]*).*/` Stema does provide some good insights in his answer though so I have accepted it. – Chris Nolet Feb 16 '12 at 14:25

2 Answers2

1

I think the only problem is to get rid of the lookbehind assertions (?<=...), they are not supported in Javascript.

The advantage of them is, you can use them to ensure that a pattern is before something, but they are NOT included in the match.

So, you need to remove them, means change (?<=v=)[a-zA-Z0-9-]+(?=&) to v=[a-zA-Z0-9-]+(?=&), but now your match starts with "v=".

If you just need to validate and don't need the matched part, then its fine, you are done.

But if you need the part after v= then put instead the needed pattern into a capturing group and continue working with those captured values.

v=([a-zA-Z0-9-]+)(?=&)

You will then find the matched substring in $1 for the first group, $2 for the second, $3 ...

stema
  • 90,351
  • 20
  • 107
  • 135
  • Great, thanks. I see what you mean with the lookbehind assertions, and this is what was giving me errors. Thanks. Any idea what the final code might look like, as a complete regex? – Chris Nolet Feb 16 '12 at 10:34
  • 1
    Try this `v=([a-zA-Z0-9-]+)(?=&)|v\\/([^&\\n]+)(?=\\?)|embed\\/([^&\\n]+)|v=([^&\\n]+)|youtu.be/([^&\\??n]+)` – stema Feb 16 '12 at 10:46
  • @stema, I have adjusted your regex so it only returns 1 match therefore no need to find which match has the ID in out of 5 matches `(?:vi?=|embed\/|vi?\/|\youtu.be\/)([^&\n]+)`, works with all urls I've seen on youtube, example here: http://rubular.com/r/ctuTE7PwrA – Rob Mar 27 '12 at 13:26
0

you can replace your look behind assertion using this post

Javascript: negative lookbehind equivalent?

Community
  • 1
  • 1