-3

I am trying to get youtube video id from a different set of Youtube URL'S. I wrote the below regex for getting video id from a set of youtube URLs. I wrote this in python.

But,

The problem is can we implement the same logic in javascript as well? Since I am quite new to javascript, I need help with it.

if data.startswith('https://') or data.startswith('http://'):
        v = re.search("(?:\/|%3D|v=|vi=)([0-9A-z-_]{11})(?:[%#?&]|$)", data)
        data="https://www.youtube.com/watch?v="+v.group(1)

*The above code is in python, data is a Youtube URL. (I have tried multiple times, but didn't work for me, Hence here.)

  • JavaScript has a `startsWith()` method on strings, just like Python does, so that logic is the same. – Barmar Jun 12 '20 at 16:48
  • [JavaScript regexp tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) – Barmar Jun 12 '20 at 16:49
  • appreciate your answer, but what is equivalent for search & group in javascript ? – shriniket deshmukh Jun 12 '20 at 16:50
  • `groups = data.match(/regexp/)`. Then use `groups[1]`. Isn't that explained in the tutorial? – Barmar Jun 12 '20 at 16:52
  • Thank you so much @Barmar , I just don't know why commmutity criticizes question, when someone is in problem ? – shriniket deshmukh Jun 12 '20 at 16:54
  • give example of match, or is it deduced from yur regax ? –  Jun 12 '20 at 16:54
  • Because SO is not a tutoring service, you're expected to try to solve the problem yourself, then come here if you can't get it working. – Barmar Jun 12 '20 at 16:54
  • @Barmar, I am not saying it at all. I am here because I didn't understand a thing in JS, hence I needed help. (PS: Why would anybody come to SO without any effort?) – shriniket deshmukh Jun 12 '20 at 16:57
  • this `[0-9A-z-_]` ? `A-z` include punct ? and [0-9A-z`-`_] is invalid range on most enjins. JS see it as a literal. sloppy –  Jun 12 '20 at 16:59
  • You say you tried multiple times, so why can't you just post what you tried, so we can explain where you went wrong? That way you'll learn better than us just spoon-feeding you the solution. – Barmar Jun 12 '20 at 17:02
  • gonna leap off say this was intent `/(?:\/|%3D|vi?=)([\w-]{11})(?=[%#?&]|$)/` –  Jun 12 '20 at 17:03

2 Answers2

0

JS has the same function so the functionality will be the same check it here JS startsWith function

Malik Türk
  • 55
  • 1
  • 8
0

The JavaScript equivalent of startswith is startsWith.

The JS equivalent of re.search() is String.prototype.match(). It returns an array containing the whole match and captured groups.

A-z is an incorrect range in the regexp. Use A-Z or a-z, along with the i modifier to make the regexp case-insensitive.

var data = 'http://www.youtube.com/watch?v=-wtIMTCHWuI';
if (data.startsWith('https://') || data.startsWith('http://')) {
  v = data.match(/(?:\/|%3D|v=|vi=)([0-9A-Z-_]{11})(?:[%#?&]|$)/i);
  if (v) {
    data = "https://www.youtube.com/watch?v=" + v[1];
  }
  console.log(data);
}
Barmar
  • 596,455
  • 48
  • 393
  • 495