Welcome to the OzoneAsylum FaqWiki |
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. 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; }
code: function stripHTML(oldString) { return oldString.replace(/<[^>]*>/g, ""); }
|