2010-01-18 90 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