Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to use the youtube data api to generate a video playlist. However, the video urls require a format of youtube.com/watch?v=3sZOD3xKL0Y, but what the api generates is youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata. So what I need to do is be able to select everything after and including the ampersand and remove it from the url. Any way to do this with javascript and some sort of regular expression?

share|improve this question
    
Take a look at this post: stackoverflow.com/questions/738351/… –  Hasan Gürsoy Sep 6 '11 at 23:21
    
possible duplicate of querystring encoding of a javascript object –  Sindre Sorhus Nov 15 '13 at 18:19

4 Answers 4

up vote 11 down vote accepted

Simple:

var new_url = old_url.substring(0, old_url.indexOf('&'));

Modified: this will remove all parameters or fragments from url

var oldURL = [YOUR_URL_TO_REMOVE_PARAMS]
var index = 0;
var newURL = oldURL;
index = oldURL.indexOf('?');
if(index == -1){
    index = oldURL.indexOf('#');
}
if(index != -1){
    newURL = oldURL.substring(0, index);
}
share|improve this answer
    
Wow. That was lightning fast. I actually discovered that I'll need to pull a different node from the feed though, that is unique. How would I extract just the id passed just after video: in this tag - tag:youtube.com,2008:video:3sZOD3xKL0Y –  mheavers Jan 10 '11 at 22:04

Example: http://jsfiddle.net/SjrqF/

var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';

url = url.slice( 0, url.indexOf('&') );

or:

Example: http://jsfiddle.net/SjrqF/1/

var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';

url = url.split( '&' )[0];
share|improve this answer

You could use a RegEx to match the value of v and build the URL yourself since you know the URL is youtube.com/watch?v=...

http://jsfiddle.net/akURz/

var url = 'http://youtube.com/watch?v=3sZOD3xKL0Y';
alert(url.match(/v\=([a-z0-9]+)/i));
share|improve this answer

Hmm... Looking for better way... here it is

var onlyUrl = window.location.href.replace(window.location.search,'');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.