简体   繁体   English

如何计算 HTML 字符串中的字数?

[英]How to count the number of words in HTML string?

I have a HTML string from a rich editor.我有一个来自富编辑器的 HTML 字符串。 I want to implement a feature in javascript that will count the number of words which are visible to user.我想在 javascript 中实现一个功能,该功能将计算用户可见的单词数。 The condition is the count should exclude the html tags and things which are not visible to the user in rich editor条件是计数应排除 html 标签和用户在富编辑器中不可见的内容

  countWords = (richHtml) => {
    var regex = /(<([^>]+)>)/ig;
    var body = richHTML.split("><").join("> <");;
    var result = body.replace(regex, "");
    var arr = result.split(" ")
    var newArr = []
    arr.forEach((el, i) => {
      if (el != "" && el != "&nbsp;" && el != "&nbsp;&nbsp;" && el != "'") {
        newArr.push(el)
      }
    })
    return newArr.length
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM