Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Javascript Regex para videos de YouTube e identificación de cortos

Quiero devolver la identificación del video de la URL del video de YouTube, incluso para Shorts .

Pero tengo un patrón que funciona para algunas URL que no incluyen Shorts

^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*

Editar: debería funcionar para las siguientes URL:

 http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s http://www.youtube.com/embed/0zM3nApSvMg?rel=0 http://www.youtube.com/watch?v=0zM3nApSvMg http://youtu.be/0zM3nApSvMg https://youtube.com/shorts/0dPkkQeRwTI?feature=share https://youtube.com/shorts/0dPkkQeRwTI

Gracias

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Esto debería funcionar en los ejemplos proporcionados.

 (youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])|$))

https://regex101.com/r/5JhmpW/1 La identificación de video real debe ser el tercer grupo de captura en cada partido.

  • Grupo 1: Url hasta la última parte
  • Group2: La última parte de la url que recibe el videoId como parámetro
  • Grupo 3: la última parte de la URL (que es el videoId en las variantes "/v/", "/embed/", "/user/", "/shorts/" y "youtu.be/"), o el parámetro videoId (en el caso de ver)

Puedes usarlo en javascript así:

 let data = `http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s http://www.youtube.com/embed/0zM3nApSvMg?rel=0 http://www.youtube.com/watch?v=0zM3nApSvMg http://youtu.be/0zM3nApSvMg https://youtube.com/shorts/0dPkkQeRwTI?feature=share https://youtube.com/shorts/0dPkkQeRwTI`; let regex = /(youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])|$))/gm; let videoIds = [...data.matchAll(regex)].map(x => x[3]);

O si solo espera una URL a la vez:

 function getVideoId(url) { let regex = /(youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])|$))/gm; return regex.exec(url)[3]; }

tenga en cuenta que las expresiones regulares en javascript no son sin estado, ejecutar la misma expresión regular varias veces hará que itere a través de las coincidencias en el texto (y eventualmente devolverá NULL una vez que haya llegado al final), razón por la cual la expresión regular se reinicializa en cada llamada en este caso. Si no se encuentran coincidencias, también devolverá nulo.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!