-1

I have a URL:

http://www.youtube.com/watch?v=JssO4oLBm2s&list=PLGHJ4fVazTpYRZTEhqgurtSH6XlDMIEJM&shuffle=382

Edit: I should also not the url is stored in a variable and I want it to work something like this:

$(".videothumb a").live('click', function() {
        var URL = < do something to cut the string > 
        console.log(URL);
        return false;
    });

And I want to cut the URL starting from "=" and ending at "&" so I'll end up with a string like this: "JssO4oLBm2s".

I only know of the slice() function but I believe that only takes a number as beginning and end points.

nbrooks
  • 17,869
  • 5
  • 51
  • 65
UzumakiDev
  • 1,266
  • 2
  • 15
  • 37

5 Answers5

1

Using .split() will give a position based solution which will fail the order of parameters changes. Instead I think what you are looking for is the value of parameter called v for that you can use a simple regex like

'http://www.youtube.com/watch?v=JssO4oLBm2s&list=PLGHJ4fVazTpYRZTEhqgurtSH6XlDMIEJM&shuffle=382'.match('[?&]v=(.*?)(&|$)')[1]
Arun P Johny
  • 376,738
  • 64
  • 519
  • 520
1

Try

'http://www.youtube.com/watch?v=JssO4oLBm2s&list=PLGHJ4fVazTpYRZTEhqgurtSH6XlDMIEJM&shuffle=382'
    .split('=')[1] // 'JssO4oLBm2s&list'
    .split('&')[0] // 'JssO4oLBm2s'

Or, if you want to be sure to get the v parameter,

var v, args = 'http://www.youtube.com/watch?v=JssO4oLBm2s&list=PLGHJ4fVazTpYRZTEhqgurtSH6XlDMIEJM&shuffle=382'.split("?")[1].split('&');
for(var i = args.length-1; i>=0; --i) {
    var data = args[i].split('=');
    if(data[0]==='v') {  v = data[1]; break;  }
}
Oriol
  • 249,902
  • 55
  • 405
  • 483
0

Use .split(). Separated to 2 lines for clarity

var first = "http://www.youtube.com/watch?v=JssO4oLBm2s&list=PLGHJ4fVazTpYRZTEhqgurtSH6XlDMIEJM&shuffle=382".split('=')[1]
var result = first.split('&')[0]; //result - JssO4oLBm2s
Venkata Krishna
  • 14,458
  • 5
  • 38
  • 56
0
v = 'JssO4oLBm2s&list=PLGHJ4fVazTpYRZTEhqgurtSH6XlDMIEJM&shuffle=382';

var vamploc = v.indexOf("&");

vstring = v.substr(0, vamploc);

You can play with the code a bit to refine it, but the general concepts work.

Oriol
  • 249,902
  • 55
  • 405
  • 483
TimSPQR
  • 2,916
  • 3
  • 19
  • 28
0

use Regexp (?:v=)(.+?)(?:&|$)

Fiddle DEMO

"http://www.youtube.com/watch?v=JssO4oLBm2s&list=PLGHJ4fVazTpYRZTEhqgurtSH6XlDMIEJM&shuffle=382".match('(?:v=)(.+?)(?:&|$)')[1]


Reference

http://gskinner.com/RegExr/

Tushar Gupta - curioustushar
  • 56,454
  • 22
  • 99
  • 107