#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    15
    Rep Power
    0

    need help with a regex expression to get youtube video id


    I've got this URL for example

    www.youtube.com/watch?v=NXtp7y24SHk&feature=g-all-u&context=G2dcd8bbF

    I need help selecting the bolded part above and remove the rest.
    I've got this code right now:

    URL.replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)(?:.*v=)(.+)\s/g, '<iframe width="360" height="250" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>');

    The problem is, it replaces everything before
    NXtp7y24SHk&feature=g-all-u&context=G2dcd8bbF
    and stops when it sees a space.
    but how can i make it remove "&feature=g-all-u&context=G2dcd8bbF" as well

    thanks.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,993
    Rep Power
    9397
    1. It's easier to match the bit you want than to remove the bits you don't want.
    2. What language are you using?
  4. #3
  5. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    This will match the bold text in numerous regex flavors:
    Code:
    (?<=watch\?v=)[^&]+
    But as requinix was asking, what flavor / language are you using?

IMN logo majestic logo threadwatch logo seochat tools logo