0

I'm trying to handel multiple different situation in the same string with regular expression. But when i'm using the code under, only the last expression will work. so dobbel stars(**) will be posted like it is, but underline(_) will be handled by the replace()function.

[HttpPost]
[ValidateInput(false)]
 public ActionResult Comment(Models.CommentModel s)
    {
        Regex fat = new Regex(@"\*\*(.*?)\*\*");
        Regex italic = new Regex(@"_(.*?)_");

       s.comment = fat.Replace(HttpUtility.HtmlEncode(s.comment), "<b>$1</b>");
       s.comment = italic.Replace(HttpUtility.HtmlEncode(s.comment), "<i>$1</i>");

       var db = new WebApplication1.Models.ApplicationDbContext();

        if (ModelState.IsValid)
        {

            if (file != null)
        {
            db.Comments.Add(s);
            db.SaveChanges();
            return RedirectToAction("Comment");
        }
        return View(s);
    }

To decrease the amount of code I have tried to merge italicand fatstring together like this: Regex(@"\*\*(.*?)\*\*|_(.*?)_"); but then i have another problem, how can Regex know the difference?

2

1 Answer 1

1

Because you are encoding the same html twice :

   // comment : sampletext **bold** _italic_ stuffsss

   s.comment = fat.Replace(HttpUtility.HtmlEncode(s.comment), "<b>$1</b>");
   // comment : sampletext <b>bold</b> _italic_ stuffsss

   s.comment = italic.Replace(HttpUtility.HtmlEncode(s.comment), "<i>$1</i>");
   // comment : sampletext &lt;b&gt;bold&lt;/b&gt; <i>italic</i> stuffsss

Simply encoding it once, when you are done with the replacing :

Regex fat = new Regex(@"\*\*(.*?)\*\*"); // btw this is called `bold` not `fat`
Regex italic = new Regex(@"_(.*?)_");

s.comment = HttpUtility.HtmlEncode(s.comment)

s.comment = fat.Replace(s.comment, "<b>$1</b>");
s.comment = italic.Replace(s.comment, "<i>$1</i>");
Sign up to request clarification or add additional context in comments.

2 Comments

thank you this worked perfectly, the only change i had to make were the last line, it needed to be above the replace() lines to work.
you are right. Indeed, it has to be above the replace, as any angle-bracket will be encoding which is not what we want.

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.