Clean text with Javascript
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Do not use JQuery. (Just Javascript)

I need to do this:

http://i.imgur.com/u9bdiom.gif

e.g. Make the function remove the HEADING tags, and all text inside the HEADING tags.
Also, be sure to trim the trailing and leading spaces from the sentences.

Here is some sample code:

https://goo.gl/HElXoK

awarded to Wikimedia via EvgeniS
Tags
javascript

Crowdsource coding tasks.

2 Solutions

Winning solution

var StrippedString = OriginalString.replace(/(<([>]+)>)/ig,"");

See example:
remove only tags - https://jsfiddle.net/nmccwjop/

remove tags and content - https://jsfiddle.net/nmccwjop/4/


This function only removes headings not every tags and also removes the spaces and tabs before them.

function fixit() {
  var str = document.getElementById("textarea_source").value;
  // remove all tags and text between tags.
  str = str.replace(/[ \t]*<h[1-6]{1}.*>(.*)<\/h[1-6]{1}>[\n\r]/ig,"");
  document.getElementById("textarea_output").value = str;
}

[ \t]* - remove all the spaces and tabs before the headings

<h[1-6]{1}.*> - start of the heading

(.*) - content of the heading

<\/h[1-6]{1}> - end of the heading

[\n\r] - remove the new line at the end of the tag if there is one

/ig - i makes it case-insensitive and g makes the regex global

View Timeline