0
用Ruby的正则表达式与HTML我怎么能代替降价格式:用正则表达式替换单词的句子在红宝石
hallo _html_ dan *gaga* sas *tes*
是
hallo <em>html</em> dan <strong>gaga</strong> sas <strong>tes</strong>
用Ruby的正则表达式与HTML我怎么能代替降价格式:用正则表达式替换单词的句子在红宝石
hallo _html_ dan *gaga* sas *tes*
是
hallo <em>html</em> dan <strong>gaga</strong> sas <strong>tes</strong>
有该媒体链接的宝石,如redcarpet
这里是一个使用默认降价渲染器的示例,但如果您需要不同的语法,则可以使用自己的渲染器。
require 'redcarpet'
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
markdown.render("**This** _is_ an [example](http://example.org/).")
# => "<p><strong>This</strong> <em>is</em> an <a href="http://example.org/">example</a>.</p>"
总的来说这是一个坏主意,使用正则表达式与HTML或推倒重来,但如果你坚持,在这里一个简单的例子,你怎么能做到这一点。
# create a hash with our regular expressions and replacements
map = { /_(.+?)_/ => '<em>\1</em>', /\*(.+?)\*/ => '<strong>\1</strong>'}
markdown = "hallo _html_ dan *gaga* das *test*"
# enumerate the hash and replace the expressions with their replacement
# tap returns the result to itself
markdown.tap{|md| map.each { |re, v| md.gsub!(re, v)}}
# => hallo <em>html</em> dan <strong>gaga</strong> das <strong>test</strong>
@Cary Swoveland嗨卡里,我是那种你,你一个风扇知道。谢谢你的建议,但你为什么不回答这个问题?我感觉就像瞄准一个移动的目标,所以我删除了它。在临睡前看到这一点,我会打电话给休息日。 – peter
的可能的复制[红宝石正则表达式替换(https://stackoverflow.com/questions/46555955/ruby-regex-replace) – colincr