OZONE Asylum
FAQ
How do I strip HTML tags using JavaScript?
This page's ID:
5782
Search
QuickChanges
Forums
FAQ
Archives
Register
You are editing "How do I strip HTML tags using JavaScript?"
Who can edit an FAQ?
Anyone registered may edit an FAQ.
Your User Name:
Your Password:
Login Options:
Remember Me On This Computer
Your Text:
Insert Slimies »
Insert UBB Code »
Close
Last Tag
|
All Tags
UBB Help
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 } else { inTag = false; i++; } } if(!inTag) newString += oldString.charAt(i); } return newString; }[/code] from: [url=http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20580452.html]Strip HTML tags from data in database rows[/url] 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(/<[^>]*>/g, ""); }[/code] from: [url=http://www.experts-exchange.com/Web/Web_Languages/Q_20577613.html]Strip HTML tags from data in database rows[/url] --------------------------- Relevant links: [url=http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20580452.html]Strip HTML tags from data in database rows[/url] - string manipulation solution. [url=http://www.experts-exchange.com/Web/Web_Languages/Q_20577613.html]Strip HTML tags from data in database rows[/url] - regular expression solution ____________________ [internallink=4626]Emperor[/internallink] [small][i](Added by: [url=http://www.ozoneasylum.com/cgi-bin/ubbmisc.cgi?action=getbio&UserName=Emperor]Emperor [/url] on Sun 10-Aug-2003)[/i][/small] [small][i](Edited by: [url=http://www.ozoneasylum.com/cgi-bin/ubbmisc.cgi?action=getbio&UserName=Emperor]Emperor [/url] on Sun 10-Aug-2003)[/i][/small];) [small](Edited by [url=http://www.ozoneasylum.com/user/6577]asela[/url] on 08-12-2007 09:54)[/small] [small](Edited by [url=http://www.ozoneasylum.com/user/6862]Irshad[/url] on 09-06-2008 11:24)[/small]
Loading...
Options:
Enable Slimies
Enable Linkwords
« Backwards
—
Onwards »