Catch string between bars

Asked

Viewed 317 times

4

I need to get the string inside the last two bars (heDA6Yu7hsc) with javascript. How to proceed?

https://i.ytimg.com/vi/heDA6Yu7hsc/img.jpg

2 answers

6


Try this way below:

var url="https://i.ytimg.com/vi/heDA6Yu7hsc/img.jpg";
var part = url.split("/");
console.log(part[4]);

Every part of array corresponds to a break of the split

[0]=https:
[1]=
[2]=i.ytimg.com
[3]=vi
[4]=heDA6Yu7hsc
[5]=img.jpg
  • Man, perfect. Thank you!!!!

  • ^^, who good q helped

  • does not forget to mark as solved

2

You can also do by regular expression.

var texto = /\/([\w\d]+)\/[\w.]+$/gi.exec("https://i.ytimg.com/vi/heDA6Yu7hsc/img.jpg");
console.log(texto[1]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.