Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I found this "[\\?&]v=([^&#]*)" on the internet can someone explain it to me

I found a method on the internet that can retrieve the Id of a youtube video from the url.

this is it.

var vid;
var results;
results = url.match("[\\?&]v=([^]*)");
vid = ( results === null ) ? url : results[1];

The Id will be contained in "vid".

What I don't understand and I find interesting and want to know is this.

results = url.match("[\\?&]v=([^]*)");

How does it work?

like image 937
Aaron de Windt Avatar asked Nov 19 '09 00:11

Aaron de Windt


People also ask

What I find or found?

Found is the past tense and past participle of the verb 'find': if you want to find (look for, search) anything, you not only ask someone such as your parent or teacher or friend, but you will find it on the Internet! Find means to discover, identify something, search for something or someone.

What I found or I have found?

'I have found' is sometimes called a 'present perfect' tense - it describes a past action with present effect. 'I found' is a past perfect, or past historic, or aorist tense (don't worry about the terminology) describing an action that's complete - over and done with.

Is found it correct grammar?

Both are correct; it just depends on what you really want to say.

Did I find or found?

Both can be correct, but mean different things. “Did you find” asks if you discovered or located something. E.g. “Did you find your car keys?”. “Did you found” asks if you established something like an group, organization, or business.


2 Answers

It's using a regular expression to extract the ID of the video from a complete URL. This particular regex breaks down as follows:

  1. [\\?&] is a character class. It matches a single character which is either & or ?. (The question mark is a special character in JavaScript regexes, so it must be escaped by preceding it with a backslash. The backslash is a special character in JavaScript strings, so it must be escaped also, hence the double backslash. That's fairly common in regular expressions.)
  2. v= matches the literal string v=.
  3. ( begins a capturing group, so everything until the next ) will be placed into a separate entry in the returned array.
  4. [^&#]* any number of characters (including none) until a & or # is found. The brackets indicate a character class as above, and the ^ inverts the class so it includes all characters except those listed before the end bracket. The * indicates that the preceding character class is to be matched zero or more times.
  5. The ) ends the capturing group.

Assuming the match is successful, results[0] contains the entire URL, and results[1] contains the contents of the first capturing group, i.e. the ID of the video.

like image 68
bcat Avatar answered Nov 15 '22 00:11

bcat


This matches the video id in a youtube url.

[\\?&]v= // finds the first ?v= or &v= in the query string

([^&#]*) // matches everything else up to the next & or #

The video id is stored in results[1] (assuming there was a match)

like image 25
Rob Avatar answered Nov 14 '22 23:11

Rob