javascript - Pulling a video id from YouTube embed code - Stack Overflow

I am trying to port over a PHP preg_match expression to pull a video ID from YouTube embed code in Java

I am trying to port over a PHP preg_match expression to pull a video ID from YouTube embed code in Javascript. I currently have this in PHP:

$pattern = '%(?:https?://)?(?:www\.)?(?:youtu\.be/| youtube\(?:/embed/|/v/|/watch\?v=))([\w-]{10,12})[a-zA-Z0-9\<\>\"]%x';
$result = preg_match($pattern, $url, $matches);

This works and returns me a video id. I have tried using the following in order to get it to work in Javascript:

var reg = /(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\(?:/embed/|/v/|/watch\?v=))([\w-]{10,12})/g;
var matches = uploadVideoEmbedCode.match(reg);

The test data I am using is:

<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>

When I try and use the above regular expression in javascript I get the following error:

Uncaught SyntaxError: Unexpected token ?

You help as ever would be greatly appreciated.

I am trying to port over a PHP preg_match expression to pull a video ID from YouTube embed code in Javascript. I currently have this in PHP:

$pattern = '%(?:https?://)?(?:www\.)?(?:youtu\.be/| youtube\.(?:/embed/|/v/|/watch\?v=))([\w-]{10,12})[a-zA-Z0-9\<\>\"]%x';
$result = preg_match($pattern, $url, $matches);

This works and returns me a video id. I have tried using the following in order to get it to work in Javascript:

var reg = /(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.(?:/embed/|/v/|/watch\?v=))([\w-]{10,12})/g;
var matches = uploadVideoEmbedCode.match(reg);

The test data I am using is:

<iframe width="560" height="315" src="http://www.youtube./embed/1NMUDb3Ewhs" frameborder="0" allowfullscreen></iframe>

When I try and use the above regular expression in javascript I get the following error:

Uncaught SyntaxError: Unexpected token ?

You help as ever would be greatly appreciated.

Share Improve this question edited May 25, 2012 at 9:44 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked May 25, 2012 at 9:40 Neil YoungNeil Young 55610 silver badges23 bronze badges 1
  • don't you think you should give credit to the answer owner on your blog? neilyoungcv./blog/code-share/… – Subliminal Hash Commented Jul 9, 2013 at 18:09
Add a ment  | 

2 Answers 2

Reset to default 5

You need to escape the / slashes in your regex as they are the delimiter. Use \/ whenever you want to match a literal /:

var reg = /(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.(?:\/embed\/|\/v\/|\/watch\?v=))([\w-]{10,12})/g;

Another option would be using new RegExp('...', 'g') instead of /.../g - but then you need to escape each backslash which would be even more annoying:

var reg = new RegExp('(?:https?://)?(?:www\\.)?(?:youtu\\.be/|youtube\\.(?:/embed/|/v/|/watch\\?v=))([\\w-]{10,12})', 'g');

To get the ID eventually, use var id = reg.exec(yourString)[1].

You have to escape the forward slashes in your regular expression: \/.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745642640a4637785.html

相关推荐

  • javascript - Pulling a video id from YouTube embed code - Stack Overflow

    I am trying to port over a PHP preg_match expression to pull a video ID from YouTube embed code in Java

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信