Welcome to the OzoneAsylum FaqWiki
Frequently Asked Questions
DHTML/JavaScript

How do I strip HTML tags using JavaScript? Differences Pages that link to <a href="https://ozoneasylum.com/backlink?for=5782" title="Pages that link to How do I strip HTML tags using JavaScript? Differences" rel="nofollow" >How do I strip HTML tags using JavaScript? Differences\

A tricky one to dig out from a search engine as there is a lot of code out there to strip JavaScript and HTML tags but there are some interesting solutions.

The first runs through the string and builds a new one stopping when it is inside a tag:


code:
function stripHTML(oldString) {

   var newString = "";
   var inTag = false;
   for(var i = 0; i < oldString.length; i++) {
   
        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
+              if(oldString.charAt(i+1)=="<")

+              {

+              		//dont do anything

-              inTag = false;
-              i++;
        }
+	else

+	{

+		inTag = false;

+		i++;

+	}

+        }

   
        if(!inTag) newString += oldString.charAt(i);

   }

   return newString;
}



from:
Strip HTML tags from data in database rows

However, some quick tests show that while most browsers are OK wih this Internet Explorer/Windows messes up every now and again.

Instead we need to run a regular expression which snips out anything within < and >:

code:
function stripHTML(oldString) {

  return oldString.replace(/<&#91;^>&#93;*>/g, "");
  
}



from:
Strip HTML tags from data in database rows

---------------------------
Relevant links:

Strip HTML tags from data in database rows - string manipulation solution.

Strip HTML tags from data in database rows - regular expression solution

____________________
Emperor

(Added by: Emperor on Sun 10-Aug-2003)

(Edited by: Emperor on Sun 10-Aug-2003)

(Edited by asela on 08-12-2007 09:54)
+(Edited by Irshad on 09-06-2008 11:24)

« BackwardsOnwards »

Show Forum Drop Down Menu