代码之家  ›  专栏  ›  技术社区  ›  NetOperator Wibby

捕获期间,但不在链接内部

  •  2
  • NetOperator Wibby  · 技术社区  · 5 年前

    我在这个好的网站上搜索了一些完整的解决方案和片段 领导 一个解决方案,没有结果。

    这是我最近得到的:

    (?!(\b[\w]+:\/\/.*?))\B\w+\.
    

    在这个片段中 Here is some sample text with a URL: http://stackoverflow.com. I would prefer to select periods outside of URLs though. 正在进行以下捕获:

    • tackoverflow.
    • om.
    • hough.

    我只想要URL后面的句号和句子中的最后一个。我正在使用javascript,所以我不能访问负lookbehind(这看起来很有用,但也许我们有一天会得到它们)。

    2 回复  |  直到 5 年前
        1
  •  2
  •   K.Dᴀᴠɪs    5 年前

    你应该能够用积极的前瞻来断言一个空间或EOL在你的周期之后。

    \.(?=\s|$)
    

    See it on Regex101

    • \. 匹配文字周期
    • (?=...) 是一个肯定的前瞻,它将声明一个空间。 \s 或EOL $ 月经过后。在url中永远不会出现这种情况,这就是它工作的原因。

    如果您想减少限制,可以用任何非字母数字字符代替空格来代替lookahead。以下是另一种选择:

    /\.(?=[^a-z0-9]|$)/igm
    

    See it on Regex101

        2
  •  0
  •   Soc    5 年前

    可以使用以下表达式: ([\w]+:\/\/[^\s]+)([\.](?=\s|$))

    将URL减去周期作为标记超链接的示例:

    const expression = /([\w]+:\/\/[^\s]+)([\.](?=\s|$))/g;
    
    const input = `http://stackoverflow.com.
    In this snippet Here is some sample text with a URL: http://stackoverflow.com. I would prefer to select periods outside of URLs though.
    http://stackoverflow.com`;
    
    const output = input.replace(expression, (match, url, period) => `[${url}](${url})${period}`);
    
    console.log(output);