js removes all tags, spaces and carriage return line breaks in html and only keeps the text

When working on a project, it is necessary to extract the text content in the HTML code, and remove characters such as spaces and carriage returns

setText(val) {
    
    
  if (val != null && val != "") {
    
    
    var re1 = new RegExp("<.+?>|&.+?;","g"); //匹配html标签的正则表达式,"g"是搜索匹配多个符合的内容
    var msg = val.replace(re1,""); //执行替换成空字符
    msg = msg.replace(/\s/g,""); //去掉所有的空格(中文空格、英文空格都会被替换)
    msg = msg.replace(/[\r\n]/g,""); //去掉所有的换行符
    return msg.substr(0, 100); //获文本文字内容的前100个字符
  } else return ''
}
// 调用示例
var html = "<p>这是一段HTML代码</p>"
setText(html)

Guess you like

Origin blog.csdn.net/weixin_44490021/article/details/128897307