使用正则表达式与Javascript

时间:2010-01-18 14:15:27

标签: javascript regex ckeditor

我正在尝试在我的一个CKEditor插件中执行此操作:

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的视频代码与输入的网址相匹配,并将其连接并连接到我的网址字符串,以便网址有效且可嵌入。

但我目前收到此错误:

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

所以我认为这是一个正常的错误,因为我不经常使用正则表达式,也许我从未见过这一个:)所以,如果有人能帮助我,那就太棒了:))

谢谢!

2 个答案:

答案 0 :(得分:7)

您正在使用javascript

不支持的'正面值守'(?<=)

答案 1 :(得分: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), '');

不是正则表达式,但是我们用我们拥有的东西做我们能做的事情;)

非常感谢!