【发布时间】:2013-07-24 13:09:50
【问题描述】:
我想从描述字符串中过滤掉标签,并将它们变成锚标签。我无法返回标签的值。
我的输入是:
a = "this is a sample #tag and the string is having a #second tag too"
我的输出应该是:
a = "this is a sample <a href="/tags/tag">#tag</a> and the string is having a <a href="/tags/second">#second</a> tag too"
到目前为止,我能够做一些小事,但我无法获得最终输出。这种模式:
a.gsub(/#\S+/i, "<a href='/tags/\0'>\0</a>")
返回:
"this is a sample <a href='/tags/\u0000'>\u0000</a> and the string is having a <a href='/tags/\u0000'>\u0000</a> tag too"
我需要做些什么不同的事情?
【问题讨论】:
-
您可以使用
nokogiri获得一些帮助。 -
@Priti 只是为了这个我不想在我的应用程序中添加 gem 的小东西
-
如果这是 HTML 解析,那么 Nokogiri 将很有用。正是 HTML 生成显着降低了 Nokogiri 的价值。