【问题标题】:How to match all strings with a specific REGEX pattern that do not start with a defined character without using a negative lookbehind如何在不使用否定后视的情况下将所有字符串与不以定义字符开头的特定 REGEX 模式匹配
【发布时间】:2021-08-11 06:01:56
【问题描述】:

我目前有一个用例,我想匹配文本中的所有 http://https:// 字符串,但前提是它们不使用 JavaScript 以 "' 开头。 如果它们以另一个字符开头,例如空格,我仍然只想匹配 http://https:// 而没有前面的字符。

我当前的正则表达式使用了否定的lookbehind,但我刚刚意识到这在 Safari 中不受支持:

/(?<!["'])(https?:\/\/)/gm

那么,使用否定的lookbehind匹配文本中的以下字符串的替代方法是什么:

  • http:// -> 应该匹配 http://
  • https:// -> 应该匹配 https://
  • xhttps:// -> 应该匹配 https:// 其中 x 可以是除 "' 之外的任何字符
  • "https:// -> 根本不应该匹配

【问题讨论】:

  • 试试/^(?!.*["']https?:\/\/).*(https?:\/\/)/gm
  • @anubhava 这将仅匹配文本中的第一个 https?://。我的目标是匹配所有https?://,即使它们位于文本中间。
  • 好的,然后使用:(?:^|[^"'])(https?:\/\/)
  • 不要看完整匹配,看捕获的组#1

标签: javascript regex negative-lookbehind


【解决方案1】:

这里不需要lookbebind,使用字符类和组:

const vars = ['http://', 'https://', 'xhttps://', '"https://']
const re = /(?:[^'"]|^)(https?:\/\/)/
vars.forEach(x => 
   console.log(x, '- >', (x.match(re) || ['',''])[1])
)

Regex:

(?:[^'"]|^)(https?:\/\/)

解释

--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    [^'"]                    any character except: ''', '"'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    ^                        the beginning of the string
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    http                     'http'
--------------------------------------------------------------------------------
    s?                       's' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
  )                        end of \1

【讨论】:

    【解决方案2】:

    你应该使用这个正则表达式:/^[a-z]+:\/\//gm

    示例

    const pattern = /^[a-z]+:\/\//gm
    const string = `http://
    https://
    xhttp://
    "http://
    'https://`
    console.log(string.match(pattern));
    // Output: [ 'http://', 'https://', 'xhttp://' ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多