Paste string between bars

4

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

https://i.ytimg.com/vi/heDA6Yu7hsc/img.jpg
    
asked by anonymous 26.09.2016 / 18:34

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]);

Each part of the generated array corresponds to a break of split

[0]=https:
[1]=
[2]=i.ytimg.com
[3]=vi
[4]=heDA6Yu7hsc
[5]=img.jpg
    
26.09.2016 / 18:37
2

You can also do it by regular expression.

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