【发布时间】:2013-06-06 22:55:30
【问题描述】:
我在以下文本中有 html 内容。
"This is my text to be parsed which contains url
http://someurl.com?param1=foo¶ms2=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>
"
需要一个将普通网址转换为超链接的正则表达式(不篡改现有的超链接)
预期结果:
"This is my text to be parsed which contains url
<a href="http://someurl.com?param1=foo¶ms2=bar">
http://someurl.com?param1=foo¶ms2=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> "
【问题讨论】:
-
正则表达式可能不是这项工作的正确工具。考虑替代方案:nokogiri.org
-
我不知道 Ruby 的正则表达式实现,但在 www.regex101.com 中可以正常工作。只要您知道可能输入的结构,正则表达式就可以了。
标签: ruby regex ruby-on-rails-3 rubular