Remove HTML Tags From String To Prevent XSS Attacks

Category: Javascript | May 10, 2018
Authoranandnat
Last UpdateMay 10, 2018
LicenseMIT
Tags
Views1,257 views
Remove HTML Tags From String To Prevent XSS Attacks

Makes use of a little bit of JavaScript and Regular Expression to remove HTML tags from strings your user typed.

How to use it:

Insert the main function into your document.

function RemoveHtmlTags(a) {
  return a.replace(/(<([^>]+)>)/ig, "");
}

Remove the HTML5 tags from a string.

var output= RemoveHtmlTags('string here');

Remove whitespace from both sides of the string:

output.trim()

You Might Be Interested In:


Leave a Reply