¿Eliminar caracteres del principio y final de '+ e.link [0] .href +'?

I've looked through all the stuff to remove characters from strings but I don't understand what's going on here.

Estoy usando un secuencia de comandos de jQuery to get videos from a Youtube channel and it displays them with links to the video. On the same page I've embedded a video using the código iframe. I set the videos to open in the target which is the iframe. However the URLs don't work.

  • URL it should be: http:// www.youtube.com/embed/VIDEO_ID
  • URL it currently is: http:// www.youtube.com/watch?v=VIDEO_ID&feature=youtube_gdata

(Broken links because new users can't post more than 2 links)

I need to remove the extra so what I wanted to do was remove http://www.youtube.com/watch?v= from the start and &feature=youtube_gdata from the end then add http://www.youtube.com/embed/ back to the start.

However the link seems to be some sort of event and I just couldn't work out how to do it.

 + e.link[0].href + 

The above is how the link appears in the javascript.

How can I make the link I need from this?

Thanks very much, Jack Clarke

preguntado el 10 de marzo de 12 a las 00:03

3 Respuestas

You can get the video id out of the old URL and construct a new URL like this:

var origURL = "http://www.youtube.com/watch?v=ayYNAEs0few&feature=youtube_gdata";
var matches = origURL.match(/(&|\?)v=([^&]+)/);
var newURL = "http://www.youtube.com/embed/" + matches[2];

// newURL == http://www.youtube.com/embed/ayYNAEs0few

Ejemplo de trabajo: http://jsfiddle.net/jfriend00/zwcgL/.

Using this technique of extracting the actual video ID from the original URL, you can then construct any type of other URL that uses it with some simple string addition.

respondido 10 mar '12, 01:03

There's probably an easier way to do what you want to do, but a really quick way would be to do something like:

var link = "http://www.youtube.com/watch?v=12345678";


// link now = 12345678

So you could use that to append to a string.

link2 = 'http://www.youtube.com/watch?v=' + link;

respondido 10 mar '12, 00:03

 var original = "http://www.youtube.com/watch?v=VIDEO_ID&feature=youtube_gdata";
 var changed = original
                 .replace( "http://www.youtube.com/watch?v=", "http://www.youtube.com/embed/")
                 .replace( "&feature=youtube_gdata", "");

 alert( changed ); // shows "http://www.youtube.com/embed/VIDEO_ID"

respondido 10 mar '12, 00:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.