Strip HTML from Text JavaScript

前端 未结 30 3587
北荒
北荒 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:22

    This should do the work on any Javascript environment (NodeJS included).

    const text = `
    
      
        
        
      
      This is some text
    `; // Remove style tags and content text.replace(/]*>.*<\/style>/gm, '') // Remove script tags and content .replace(/]*>.*<\/script>/gm, '') // Remove all opening, closing and orphan HTML tags .replace(/<[^>]+>/gm, '') // Remove leading spaces and repeated CR/LF .replace(/([\r\n]+ +)+/gm, '');

提交回复
热议问题