首页 文章

正则表达式匹配本地降价链接

提问于
浏览
1

我正在尝试创建一个匹配markdown url的正则表达式,但忽略它之前和之后的内容 . 它应仅匹配指向本地文件的本地markdown网址,并忽略指向外部网站的网址 . 例:

"dddd [link which should be ignore](http://google.com/) lorem ipsum lorem ips sum loreerm [link which shouldn't be ignored](../../../filepath/folder/some-other-folder/another-folder/one-last-folder/file-example.html). lorem ipsum lorem"

应该只匹配第二个链接 . 目前,它匹配一切 . 我的正则表达式适用于我需要的东西,但这似乎是我发现的主要优势 .

到目前为止我所拥有的:

/(!?\[.*?\]\((?!.*?http)(?!.*?www\.)(?!.*?#)(?!.*?\.com)(?!.*?\.net)(?!.*?\.info)(?!.*?\.org).*?\))/g

目前,这忽略了第一个链接并匹配第二个链接,如果第二个链接在第一个链接之后没有出现 . 否则,它匹配从第一个到第二个的所有内容 .

我正在使用JavaScript,它不支持负面的lookbehinds . 有什么建议?

2 回答

  • 0

    有两个问题 .

    • \[.*?\] 将会超过 ] 并匹配 [link which should be ignore](http://google.com/) lorem ipsum lorem ips sum loreerm [link which shouldn't be ignored] ,这样它就会匹配断言 .

    • 这些断言是无限的 .

    您可以使用此正则表达式修复1和2

    ((!?\[[^\]]*?\])\((?:(?!http|www\.|\#|\.com|\.net|\.info|\.org).)*?\))

    Expanded

    (                             # (1 start)
          ( !?\[ [^\]]*? \] )           # (2), Link
          \(                            # Open paren (
          (?:                           # Cluster
               (?!                           # Not any of these
                    http
                 |  www\.
                 |  \# 
                 |  \.com 
                 |  \.net 
                 |  \.info 
                 |  \.org 
               )
               .                             # Ok, grab this character 
          )*?                           # End cluster, do 0 to many times
          \)                            # Close paren )
     )                             # (1 end)
    

    度量

    ----------------------------------
     * Format Metrics
    ----------------------------------
    Cluster Groups      =   1
    
    Capture Groups      =   2
    
    Assertions          =   1
           ( ? !        =   1
    
    Free Comments       =   7
    Character Classes   =   1
    
  • 1

    测试url是本地还是外部是 not 正则表达式的工作 . 正如您在示例字符串中的第三个链接所看到的那样,测试uri是否包含 .org.comhttp ,_ # 或其他任何错误 .

    此代码显示如何在客户端的替换上下文中知道url是否为本地URL:

    var text = '[external link](http://adomain.com/path/file.txt) ' +
               '[local link](../path/page.html) ' +
               '[local link](../path.org/http/file.com.php#fragment)';
    
    text = text.replace(/\[([^\]]*)\]\(([^)]*)\)/g, function (_, g1, g2) {
        var myurl = document.createElement('a');
        myurl.href = g2;
        return window.location.hostname == myurl.hostname ? "locrep" : "extrep"; 
    });   
    
    console.log(text);
    

相关问题