我想過濾一個描述字符串中的標籤,並希望將它們製作成錨標籤。我無法返回標籤的值。如何使用gsub搜索並使用正則表達式進行替換?
我輸入的是:
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的價值。 –