ruby - 我需要一个正则表达式来找到一个不在任何 html 标签或任何 html 标签的属性值内的 url

标签 ruby regex ruby-on-rails-3 rubular

我在下面的文本中有 html 内容。

    "This is my text to be parsed which contains url 
    http://someurl.com?param1=foo&params2=bar 
 <a href="http://thisshouldnotbetampered.com">
    some text and a url http://someotherurl.com test 1q2w
 </a> <img src="http://someasseturl.com/abc.jpeg"/>
    <span>i have a link too http://someurlinsidespan.com?xyz=abc </span> 
    "

需要一个将纯 url 转换为超链接的正则表达式(不篡改现有超链接)

预期结果:

    "This is my text to be parsed which contains url 
    <a href="http://someurl.com?param1=foo&params2=bar">
http://someurl.com?param1=foo&params2=bar</a> 
 <a href="http://thisshouldnotbetampered.com">
    some text and a url http://someotherurl.com test 
1q2w </a> <img src="http://someasseturl.com/abc.jpeg"/>
    <span>i have a link too <a href="http://someurlinsidespan.com?xyz=abc">http://someurlinsidespan.com?xyz=abc</a> </span> "

最佳答案

Disclaimer: You shouldn't use regex for this task, use an html parser. This is a POC to demonstrate that it's possible if you expect a good formatted HTML (which you won't have anyway).

这就是我想出的:
(https?:\/\/(?:w{1,3}.)?[^\s]*?(?:\.[a-z]+)+)(?![^<]*?(?:<\/\w+>|\/?>))

这是什么意思?

  • ( : 第一组
  • https? : 匹配httphttps
  • \/\/ : 匹配//
  • (?:w{1,3}.)? : 可选地匹配 w. , ww.www.
  • [^\s]*? : 匹配除空格之外的任何内容零次或多次 ungreedy
  • (?:\.[a-z]+)+) : 匹配后跟 [a-z] 的点字符,重复一次或多次
  • (?! : 负前瞻
    • [^<]*? : 匹配除 < 以外的任何内容零次或多次不贪心
    • (?:<\/\w+>|\/?>) : 匹配结束标签或 />>
    • ) : 前瞻结束
  • ) : 第一组结束


regex101 online demo rubular online demo

关于ruby - 我需要一个正则表达式来找到一个不在任何 html 标签或任何 html 标签的属性值内的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17038220/

相关文章:

ruby - 在ruby中将字符串乘以整数vs整数乘以字符串

ruby-on-rails - 转换updated_at日期格式

python - 如何从 python 正则表达式中排除特定字符串

c# - 什么正则表达式将匹配限制为 6 个小数位的小数值?

regex - Linux重命名命令首字母大写

mysql - 使用 Mysql2 for Ruby

ruby - 使用 Ruby 将输入值适本地转换为整数或 float

ruby - 使写入文件线程安全

sql - 如何访问 Rails3 中的连接表属性?

css - chardinjs 不能在 ruby​​ on rails 中工作