Convert HTML to plain text in JS without browser environment

前端 未结 6 1358
孤街浪徒
孤街浪徒 2020-12-29 23:59

I have a CouchDB view map function that generates an abstract of a stored HTML document (first x characters of text). Unfortunately I have no browser environmen

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 00:25

    It's pretty simple, you can also implement a "toText" prototype:

    String.prototype.toText = function(){
        return $(html).text();
    };
    
    //Let's test it out!
    var html = "link 
    TEXT"; var text = html.toText(); console.log("Text: " + text); //Result will be "link TEXT"

提交回复
热议问题