Strip out HTML tags in JavaScript

November 17, 2016

Tags: ServiceNow Strip HTML tags Server Script JavaScript

In cases where you’re getting information from external sources, you may need to strip out HTML tags.

The code below does the following:

  1. Removes all HTML tags over multiple lines
  2. Splits the string into an array of strings, each string represents a line
  3. All   codes are replaced with their text alternatives
  4. Empty lines are removed
  5. Turn the array back into a string and return the result
function stripHtml(html) {
    return html.replace(/<(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*>/g, '')
        .split(/\n/)
        .map(function(line) {
    	       return line.replace(/(&nbsp;)/g, ' ').trim();
        }).filter(function(line) {
    	       return line != '' && line != '&nbsp;';
        })
        .join('\n');
}

Regular expression is taken from this StackOverflow post

Comments

comments powered by Disqus