2010-01-18 140 views
0

我试图做这个小东西放到我的CKEditor的插件之一:使用正则表达式使用Javascript

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&#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); 
    } 
} 

什么是应该做的事:匹配YouTube的视频代码转换成所输入的网址,并把它和连接到我的网址字符串,以便URL有效且可嵌入。

但我目前得到这个错误:

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

所以我认为这是一个正常的错误,因为我不玩的正则表达式很多时候也许我从来没有见过这样一个:)所以,如果有人可以帮助我会很棒:)

谢谢!

回答

0

下面是我终于做到了:

// 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), ''); 

不是一个正则表达式,但诶我们做什么,我们能做的与我们有什么;)

谢谢反正!

7

您使用的是“积极的回顾后” (?<=)这是不被JavaScript支持

+0

OH!废话。我想我必须找到另一种表情。 – TomShreds 2010-01-18 14:22:50

+0

哎呀。我认为几乎所有不支持基础先进RE的味道现在都已经很久没有... – Joey 2010-01-18 14:26:25

+0

@Tom:只要使用'/ \?v =([ - a-zA-Z0-9 _-] +)/ i'向后看的断言。 – Gumbo 2010-01-18 14:28:29