Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6189967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:30:15+00:00 2026-05-24T02:30:15+00:00

I am using Html.Raw(Html.Encode()) to allow some of html to be allowed. For example

  • 0

I am using Html.Raw(Html.Encode()) to allow some of html to be allowed. For example I want bold, italic, code etc… I am not sure it’s the right method, code seems pretty ugly.

Input

Hello, this text will be [b]bold[/b]. [code]alert(“Test…”)[/code]

Output

enter image description here

Code

    @Html.Raw(Html.Encode(Model.Body)
    .Replace(Environment.NewLine, "<br />")
    .Replace("[b]", "<b>")
    .Replace("[/b]", "</b>")
    .Replace("[code]", "<div class='codeContainer'><pre name='code' class='javascript'>")
    .Replace("[/code]", "</pre></div>"))

My Solution

I want to make it all a bit different. Instead of using BB-Tags I want to use simpler tags.
For example * will stand for bold. That means if I input This text is *bold*. it will replace text to This text is <b>bold</b>.. Kinda like this website is using BTW.

Problem

To implement this I need some Regex and I have little to no experience with it. I’ve searched many sites, but no luck.

My implementation of it looks something like this, but it fails since I can’t really replace a char with string.

static void Main(string[] args)
{
    string myString = "Hello, this text is *bold*, this text is also *bold*. And this is code: ~MYCODE~";
    string findString = "\\*";
    int firstMatch, nextMatch;

    Match match = Regex.Match(myString, findString);

    while (match.Success == true)
    {
        Console.WriteLine(match.Index);
        firstMatch = match.Index;
        match = match.NextMatch();
        if (match.Success == true)
        {
            nextMatch = match.Index;

            myString = myString[firstMatch] = "<b>"; // Ouch!
        }
    }

    Console.ReadLine();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:30:15+00:00Added an answer on May 24, 2026 at 2:30 am

    To implement this I need some Regex

    Ah no, you don’t need Regex. Manipulating HTML with Regex could lead to some undesired effects. So you could simply use MarkDownSharp which by the way is what this site uses to safely render Markdown markup into HTML.

    Like this:

    var markdown = new Markdown();
    string html = markdown.Transform(SomeTextContainingMarkDown);
    

    Of course to polish this you would write an HTML helper so that in your view:

    @Html.Markdown(Model.Body)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was playing around with some ideas using raw html and JQuery. One thing
I'm using HTML emails for a client's newsletter. Not using HTML mails is not
I am using html, javascript & mod_python. I want to submit html form. To
I'm working on sanitizing my Html using Jeff Atwood's code found here But the
I am using ASP.NET MVC 2 & C#. I want to include/embed an html
I want to clean an HTML page of its tags, using Ruby. I have
I am using lxml.html to parse some hmtl to get links, however when it
Using HTML and CSS. I have text surrounded by a border using display:inline to
Using html and css for IE7, how do I position a html table at
Using HTML 5 together with the DOM is there a way to get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.