我想斜体添加到正则表达式匹配的文本,但它的失败:红宝石的正则表达式解析强调斜体
string = 'this should have _emphasis_ but this_one_should_not'
string.gsub!(%r{ (\*|_) (\S|\S.*?\S) \1 }x, %{<em>\\2</em>})
string.should == 'this should have <em>emphasis</em> but this_one_should_not'
# actual = 'this should have <em>emphasis</em> but this<em>one</em>should_not'
中间用斜体一个错误被打开斜体。我从其他地方复制了这段代码,但我需要调整它以使其适用于此用例。
你正在处理包含Markdown格式的文本吗?如果是这样,为什么不使用宝石来转换它?它会自动添加格式。 –
,因为我只需要支持一小组markdown(em,strong,links等),我找不到任何可以选择我想要转换的元素的选项。 – Andrew