I’m trying to add italics to text that matches a regular expression, but it’s failing:
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'
The one with italics in the middle is incorrectly being turned italic. I copied this code from somewhere else, but I need to adjust it so that it works for this use case.
Here’s one that works:
And here’s a demo.