2

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.)

0

3 Answers 3

5

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>");
Sign up to request clarification or add additional context in comments.

Comments

2

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>");

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.