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?

elektronet
  • 108
  • 13

1 Answers1

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>");
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • 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. – elektronet Apr 28 '16 at 20:31
  • you are right. Indeed, it has to be above the replace, as any angle-bracket will be encoding which is not what we want. – Xiaoy312 Apr 28 '16 at 20:36