Strip HTML from Text JavaScript

前端 未结 30 3576
北荒
北荒 2020-11-21 05:08

Is there an easy way to take a string of html in JavaScript and strip out the html?

30条回答
  •  后悔当初
    2020-11-21 05:23

    I made some modifications to original Jibberboy2000 script Hope it'll be usefull for someone

    str = '**ANY HTML CONTENT HERE**';
    
    str=str.replace(/<\s*br\/*>/gi, "\n");
    str=str.replace(/<\s*a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 (Link->$1) ");
    str=str.replace(/<\s*\/*.+?>/ig, "\n");
    str=str.replace(/ {2,}/gi, " ");
    str=str.replace(/\n+\s*/gi, "\n\n");
    

提交回复
热议问题