Check if a paragraph starts with a character and apply an HTML style on it if so

I'm using a textarea to write a text like this:

%this is my text

%%also text

simple text

%%%last text

If a paragraph starts with %, it should show the whole line with <h1>, if there are 2, %%, it should be <h2> and so on until <h3>.

For the above example it must return:

<h1>this is my text</h1>

<h2>also text</h2>

simple text

<h3>last text</h3>

I've done this:

  var result = inputVal.replace('%', '<h1>');

it replaces the symbol at the beginning but I don't know how to add the closing tag at the end of the line. And also how to count how many % are so it can show , or other ones.

Is the a way to do this?

Read more here:

Content Attribution

This content was originally published by Leo Messi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: