Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove HTML from a string (in JSON response)

Tags:

javascript

var json = {
    "Item": {
        "Items": {
         "Body": "<SPAN style=\"LINE-HEIGHT: 115%; FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA\">\"We have all been inoculated with Christianity, and are never likely to take it seriously now! You put some of the virus of some dreadful illness into a man's arm, and there is a little itchiness, some scratchiness, a slight discomfort, disagreeable, no doubt, but not the fever of the real disease, the turning and the tossing, and the ebbing strength. And we have all been inoculated with Christianity, more or less. We are on Christ's side, we wish him well, we hope that He will win, and we are even prepared to do something for Him, provided, of course, that He is reasonable, and does not make too much of an upset among our cozy comforts and our customary ways. But there is not the passion of zeal, and the burning enthusiasm, and the eagerness of self-sacrifice, of the real faith that changes character and wins the world.\"<B>A.J. Gossip<\/B><\/SPAN>",
        },

    }
};
var someString = json.Item.Body.replace(/<br\s*\/?\s*>/g,"\n");   
alert(someString);
​

I am getting HTML tags in my response, which i should remove or parse it. How can i go about this. I did for BR tags, but now it seems there are other tags too coming in.

like image 335
John Cooper Avatar asked Aug 02 '12 19:08

John Cooper


People also ask

How escape HTML tag JSON?

You're HTML values are OK, but the keys of the JSON object must be enclosed in quotes. string. quotation marks. Also, if you output this JSON object inside the script tags of an HTML page, you must escape the "</" sequence of HTML closing tags, as per this appendix in the HTML 4 specification.

How do I strip a string in HTML?

To strip out all the HTML tags from a string there are lots of procedures in JavaScript. In order to strip out tags we can use replace() function and can also use . textContent property, . innerText property from HTML DOM.

How do I remove a property from a JSON object?

To remove JSON element, use the delete keyword in JavaScript.


2 Answers

I think all you need is a simple regex to strip html style tags from the content. Try this.

str.replace(/<\/?[^>]+>/gi, '')
like image 117
Segu Avatar answered Oct 04 '22 05:10

Segu


This should do the magic http://jsfiddle.net/Ygfvp/ Anyway it won't strip the html comments

like image 38
G.G. Avatar answered Oct 04 '22 04:10

G.G.