I would like to save a substring to a javascript variable using regex unless there is a different/easier way. For example i have a link like this: http://www.youtube.com/watch?v=sEHN4t29oXY&feature=related
I want to only get sEHN4t29oXY&feature=related so i guess i would have to check for the first equal sign to appear and after that save the rest of that string into the variable.. please help, thanks
Efficient:
variable = variable.substring(variable.indexOf('?v=')+3) // First occurence of ?v=
Regular expression:
variable = variable.replace(/.*\?v=/, '') // Replace last occurrence of ?v= and any characters before it (except \r or \n) with nothing. ? has special meaning, that is why the \ is required
variable = variable.replace(/.*?\?v=/, '') // Variation to replace first occurrence.
Like so:
var match = /\?v=(.+)/.exec(link)[1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With