5
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.

2
  • 2
    If you just want to "slap the markup in the DOM" then jQuery (or direct innerHTML tricks) should work -- but bear in mind possible injection attacks for non-controlled sources. If you want to take the HTML and turn it into something else .. well, what do you have (and why) and what do you want? :) Commented Aug 2, 2012 at 19:47
  • Now, what do you want - parse or remove? Or parse to remove them? Where and for what do you need the result? Commented Aug 2, 2012 at 20:25

3 Answers 3

21

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

str.replace(/<\/?[^>]+>/gi, '')
Sign up to request clarification or add additional context in comments.

1 Comment

the i is redundant, right? not a big deal but the extra "check upper and lower case" on lots of checks would add up
1

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

1 Comment

the Segu one will. /<\/?[^>]+>/gi (with the escaped slash)
1

If you're mapping Json using React, then you can just leave the html tags and put them to use.

Instead of:

<div>{el.content.rendered}</div>

Do this:

<div dangerouslySetInnerHTML={{__html: el.content.rendered}}></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.