使用 URI::regexp 定位不区分大小写的方案?

Target case-insensitive schemes with URI::regexp?

我有这段代码可以使用这些方案删除 URI:

htmldoc.gsub(/#{URI::regexp(['http', 'https', 'ftp', 'mailto'])}/, '')

但是,它不会检测像 HTTPHttp 这样的大写 URI,除非我将它们添加到数组中。

我尝试将不区分大小写的标志 i 添加到正则表达式中,但没有成功。

知道如何实现吗?

URI::regexp calls the default parser's make_regexp which in turn passes the given arguments to Regexp::union 并根据其文档:(强调我的)

The patterns can be Regexp objects, in which case their options will be preserved, or Strings.

应用于您的问题:

pattern = URI::regexp([/http/i, /https/i, /ftp/i, /mailto/i])

htmldoc = <<-HTML
<html>
<body>
  <a href="https://example.com">here</a>
  <a href="HTTPS://example.com">here</a>
</body>
</html>
HTML

puts htmldoc.gsub(pattern, '')

输出:

<html>
<body>
  <a href="">here</a>
  <a href="">here</a>
</body>
</html>