Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:50:15+00:00 2026-06-04T20:50:15+00:00

I am trying to port over a PHP preg_match expression to pull a video

  • 0

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\.com(?:/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\.com(?:/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.com/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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:50:16+00:00Added an answer on June 4, 2026 at 8:50 pm

    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\.com(?:\/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\\.com(?:/embed/|/v/|/watch\\?v=))([\\w-]{10,12})', 'g');
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to port over some Core Data code over from a test Mac
I'm trying to port some PHP code over to C#, and I ran across
I'm trying to port over a tarot fortune card reading game from the iPhone
I'm trying to port code over to compile using Microchip's C18 compiler for a
I am trying to port my working code from Mac OS X to GNU/Linux.
I'm trying to port a script from R into PHP but not sure what
I'm trying to port the following php functionality over to perl: public function loadKey($mod,
Trying to port java code to C++ I've stumbled over some weird behaviour. I
I'm trying to port a database from SQL Server Express over to my MySQL
Currently I'm trying to port a web application(ASP.NET MVC) to windows azure and have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.