2012-08-02 2 views
4
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); 
​ 

я получаю HTML теги в моем ответе, который я должен удалить или разобрать его. Как я могу это сделать. Я сделал для BR тегов, но теперь, похоже, есть и другие теги.удалить HTML из строки (в ответ JSON)

+2

Если вы просто хотите, чтобы «хлопнуть разметку в DOM», то JQuery (или прямые приемы innerHTML) должны работать - - * но * иметь в виду возможные * инъекционные атаки * для неконтролируемых источников. Если вы хотите взять HTML и превратить его во что-то еще ... ну, что у вас есть (и * почему *) и что вам нужно? :) –

+0

Теперь, что вам нужно - разобрать или удалить? Или проанализировать, чтобы удалить их? Где и для чего вам нужен результат? – Bergi

ответ

17

Я думаю, вам нужно простое регулярное выражение, чтобы разделить html-теги стиля из контента. Попробуй это.

str.replace(/<\/?[^>]+>/gi, '') 
+0

'i' является избыточным, не так ли? не большое дело, но дополнительный «чек верхний и нижний регистр» по множеству проверок должен был бы добавить – ErichBSchulz

0

Это должно сделать волшебную http://jsfiddle.net/Ygfvp/ Во всяком случае, не будет обирать HTML комментарии

+0

Segu one will. '/ <\/?[^>] +>/gi' (с экранированной косой чертой) –

Смежные вопросы