-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
  • 18,126
  • 5
  • 54
  • 66
UzumakiDev
  • 1,286
  • 2
  • 17
  • 39

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
  • 384,651
  • 66
  • 527
  • 531
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
  • 274,082
  • 63
  • 437
  • 513
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,926
  • 5
  • 42
  • 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
  • 274,082
  • 63
  • 437
  • 513
TimSPQR
  • 2,964
  • 3
  • 20
  • 29
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
  • 58,085
  • 24
  • 103
  • 107