Using Regex with Javascript
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/shock开发者_高级运维wave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&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!
You are using a 'positive lookbehind' (?<=)
which is not supported by javascript
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!
精彩评论