【问题标题】:URL matching regexp does not work all the timeURL匹配正则表达式并不总是有效
【发布时间】:2014-06-03 05:40:40
【问题描述】:

任何人都可以很好地了解正则表达式,请向我解释为什么这个正则表达式不适用于我的示例文本?或者更好的是,想出一个真正有效的正则表达式。

/((?:(?:ht|f)tps?:\/\/|www)[^<>\]]+?(?![^<>\]]*([>]|<\/))(?=[\s!,\]]|$))/igm

正则表达式来自这个问题:URL replace with anchor, not replacing existing anchors

示例文字:

"This is a simple test. \r\n\r\n" + 
"<u><b>A bold header with underscore:<\/b><\/u>\r\n" + 

"<i>THESE AREN'T TRANSFORMED. WHY??<\/i>\r\n" + 
"* http:\/\/www.example.net\/edit.php\r\n" + 
"* http:\/\/www.example.net\/test.php -> Some text\r\n" + 

"<i>THESE ARE:<\/i>\r\n" + 
"* http:\/\/www.example.net\/invite.php\r\n" + 
"* http:\/\/www.example.net\/forums.php\r\n"

Complete example of problem here

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    这是我想出的:

    ((?:(?:ht|f)tps?:\/\/|www)[^<>\]]+?(?![^<>\]]*(><\/))(?=[\s!,\]]|$))/igm
    

    我用你的字符串测试了它,它正在工作......

    我将您的旧正则表达式 ([&gt;]|&lt;\/) 更改为 (&gt;&lt;\/) ...您正在执行 OR 操作,在这种情况下您需要有可能匹配所有内容。

    输出如下:

    • www.example.net/edit.php\r\n"
    • www.example.net/test.php
    • www.example.net/invite.php\r\n"
    • www.example.net/forums.php\r\n"

    我希望这是你需要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      相关资源
      最近更新 更多