Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
Fetching YT video, channel or none

Given a url list, how can I divide it to 3 sum lists?

one for YT videos, second for YT channels, third for all the rest?

const paragraph1 = 'www.youtube.com/watch?v=NsjeEt1ZpqQ';
const regex1 = /www.youtube.com/(\c*)(watch?v=)?<videoId>[A-Z,0-9])/gi;

const paragraph2 = 'https://www.youtube.com/channel/UCKqFqiCe1dCUxRe0_YNZ6gg';                                 
const regex2 = /www.youtube.com/channel/?<channelId>[A-Z,_,0-9])/gi;

const found = paragraph1.match(regex1);
console.log(found);
// expected output: Array ["T", "I"]

const found = paragraph2.match(regex2);
console.log(found);

Tried to sandbox on this site.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since you are planning to split some URL string list into three different parts, you can use three different patterns:

www\.youtube\.com\/watch\?v=(?<videoId>\S+)
www\.youtube\.com\/channel\/(?<videoId>\S+)
www\.youtube\.com(?!\/(?:channel\/|watch\?v=))\S*

See regex #1, regex #2 and regex #3 demos. Note you need an ECMAScript 2018+ compliant JavaScript environment for the named capturing groups to work. Also, see the dots are escaped everywhere they denote literal dots.

The patterns mean

  • www\.youtube\.com\/watch\?v= - a literal www.youtube.com/watch?v= string
  • (?<videoId>\S+) - Group "videoId": one or more non-whitespace chars
  • www\.youtube\.com\/channel\/(?<videoId>\S+) - a literal www.youtube.com/channel/ string and then Group "videoId" capturing one or more non-whitespace chars
  • www\.youtube\.com(?!\/(?:channel\/|watch\?v=))\S* - www.youtube.com string and then a negative lookahead that fails the match if, immediately to the right, there is a / char, then channel/ or watch?v=, and then zero or more non-whitespace chars are consumed.

If you plan to use the patterns agains some mark-up text, make sure you subtract the mark-up chars from the \S pattern, that is, change it into a negated character class with a reverse shorthand, [^\s], and add the chars after \s. Say, if the links are inside double quotes, put " there, [^\s"].

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda