views:

56

answers:

2

Hi, I'm trying to do this little something into one of my CKEditor plugins:

onOk:function(){
    var sInsert=this.getValueOf('info','insertcode_area');
    if ( sInsert.length > 0 ) {
    regex = new RegExp('(?<=\?v=)([-a-zA-Z0-9_-]+)', 'gi');

    url = 'http://www.youtube.com/v/'+sInsert.match(regex);
        sInsert = '<object type="application/x-shockwave-flash" data="'+url+'" width="425" height="350"><param name="movie" value="'+url+'" /><a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&amp;#038;promoid=BIOW" target="blank"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get flash player to play to this file" width="88" height="31" /></a><br /></object>';

        e.insertHtml(sInsert);
  }
}

What it is supposed to do: Match the YouTube's video code into the entered URL and take it and concatenate to my url string so the URL is valid and embeddable.

But I currently get this error:

invalid quantifier ?<=?v=)([-a-zA-Z0-9_-]+)

So I supposed it is a normal error and since I don't play with regex very often maybe I never saw this one :) So if someone could help me it would be great :)

Thanks!

+7  A: 

You are using a 'positive lookbehind' (?<=) which is not supported by javascript

Diadistis
OH! Crap. I guess I'll have to find another expression..
Tom
Yikes. And I thought nearly all flavors not supporting even basic advanced RE are long dead by now ...
Joey
@Tom: just use `/\?v=([-a-zA-Z0-9_-]+)/i` without the lookbehind assertion.
Gumbo
A: 

Here is how I finally did it:

// First I replace those, so it'll become a valid YouTube embeddable URL.    
url = sInsert.replace('watch?v=', 'v/');
// Then I remove all the crap after the URL.
url = url.replace(url.substr(url.indexOf('&', 0), url.length), '');

Not a regex, but eh we do what we can do with what we have ;)

Thanks anyways!

Tom