1

I need to strip out the IDs of embedded youtube videos, so I have the url which is something like:

www.youtube.com/embed/[someID]&rel=0&controls=0&showinfo=0&frameborder=1&modestbranding=1

All I want is the [someID] string. I have declared an empty array to store the regex matches;

var videoID = [];

The closest I have come to a solution is:

videoID = videoID.match("embed/(\w*)");

but this results in the following:

video[0] ("embed/")
video[1] ()
1
  • jQuery isn't a language. You're writing JavaScript. JS doesn't have any builtin URL parsing capabilities, so you'll have to do some sort of string manipulations: url.split('&')[0].split('/').slice(-1)[0] Commented Jan 20, 2014 at 11:06

3 Answers 3

1

Use this:

url = "www.youtube.com/embed/someID&rel=0&controls=0&showinfo=0&frameborder=1&modestbranding=1";

Then use either:

var videoID = url.match(/embed\/(\w*)/); // regex 

OR else:

var videoID = url.match("embed/\\w*)"); // regex object

Both will give this output:

["embed/someID", "someID"]

If you provide a string then String#match method will attempt to construct a RegExp object and for that case you need to use \\w instead of \w.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this you may get id. I didn't use regex but still we can get id from above demo like urls.

var url ="www.youtube.com/embed/[someID]&rel=0&controls=0&showinfo=0&frameborder=1&modestbranding=1";

var urlQueries = url.split('/'); 
var queryParameters = urlQueries[2].split('&'); // arrays of all query parameters
var id = queryParameters[0]; // get ids at index 0

Working demo here

Comments

0

I'm not sure if can do Positive-Lookbehinds, but try this (?<=embed\/)([^&]*)

If not, then (?:embed\/)([^&]*) is closer to your example.


Demo:

http://regex101.com/r/nW5uL8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.