hey I am currently trying to replace html in a string. I.e <strong>text</strong> needs to be <b>text</b>
etc. (I realize the b tag is considered outdated)
I am aware that I shouldn’t use regex to fix this, but this is currently my only option
my code:
//replace strong
text = Regex.Replace(text, "<strong>.*?</strong>", "<b>$1</b>");
//replace em
text = Regex.Replace(text, "<em>.*?</em>", "<i>$1</i>");
the issue here is that the regex replaces the tags and sets the text to $1. how to avoid this?
(I’m in C# btw.)
The
$1will use the value of the first capture in the match. But you have not specified any capturing groups in the match, so there is nothing for$1to subtitute.Use
(…)to capture in a regex expression: