我想从描述字符串中过滤出标签,并希望将它们变成锚定标签。我无法返回标签的值。
我的输入是:
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"
我需要做些什么?
您可以这样做:
a.gsub(/#(\S+)/, '<a href="/tags/\1">\0</a>')
替换无效的原因是,在双引号之间时必须使用双转义:
a.gsub(/#(\S+)/, "<a href='/tags/\\1'>\\0</a>")
请注意,/i
此处不需要修饰符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句