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 italic
and fat
string together like this: Regex(@"\*\*(.*?)\*\*|_(.*?)_");
but then i have another problem, how can Regex know the difference?