【问题标题】:Using Regex with Javascript在 Javascript 中使用正则表达式
【发布时间】:2010-01-18 14:15:26
【问题描述】:

我正在尝试在我的一个 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 的视频代码与输入的 URL 匹配,并将其连接到我的 url 字符串,这样 URL 是有效且可嵌入的。

但我目前收到此错误:

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

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

谢谢!

【问题讨论】:

    标签: javascript regex ckeditor


    【解决方案1】:

    您正在使用 javascript 不支持的 'positive lookbehind' (?&lt;=)

    【讨论】:

    • 哎呀。而且我认为现在几乎所有不支持基本高级 RE 的口味都早已死去......
    • @Tom:只需使用 /\?v=([-a-zA-Z0-9_-]+)/i 而不使用后向断言。
    【解决方案2】:

    这是我最终做到的:

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

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

    还是谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多