javascript - Extract text from a ajax response which contains <tag> -


i have write little piece of code retrieve news rss page. here code :

this.loadrecentnews = function loadrecentnews() {             $.get("http://rss.nytimes.com/services/xml/rss/nyt/globalhome.xml", function (data) {                 $(data).find("item").each(function () {                     var el = $(this);                      console.log("------------------------");                     console.log("title      : " + el.find("title").text());                     console.log("link     : " + el.find("link").text());                     console.log("description: " + el.find("description").text());                     console.log("date: " + el.find("pubdate").text());                  });             });          }; 

here output :

title : example of title  link : http://www.example.com  description : example of <<bb>>description</b> containing <> tag..  date : example of date 

my problem want extract text of description value build new json object contain text.

how can extract text without <> values ?

seeing you're using jquery, can use .text() method.

var beforetext = "example of <b>description</b>" ; var aftertext = $("<p>").html(beforetext).text() ; 

this uses jquery make new html element (that never added page) , update innerhtml , use .text() text only.

another (riskier) option regex replace between < , > empty string:

var aftertext = beforetext.replace(/<[^>]*>+/g, "") 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -