Reputation: 169
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.)
Upvotes: 2
Views: 1422
Reputation: 627488
You can also consider using:
text = Regex.Replace(text, "<strong>(.*?)</strong>", "<b>$1</b>", RegexOptions.Singleline);
Note that RegexOptions.Singleline
is necessary to allow .
to match line feed (LF, \x0A
) characters, that the .
pattern cannot match by default.
Upvotes: 0
Reputation: 14929
Note that the following answer is just a workaround; it is better to write a proper regex.
var text = "<strong>asfdweqwe121</strong><em>test</em>";
text = text.Replace("<strong>", "<b>");
text = text.Replace("</strong>", "</b>");
text = text.Replace("<em>", "<i>");
text = text.Replace("</em>", "</i>");
Upvotes: 2
Reputation: 109190
The $1
will 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 $1
to subtitute.
Use (…)
to capture in a regex expression:
text = Regex.Replace(text, "<strong>(.*?)</strong>", "<b>$1</b>");
Upvotes: 5