代码之家  ›  专栏  ›  技术社区  ›  Dipesh Desai

使用REGEX[duplicate]在node js中提取HTML文档的<BODY>文本

  •  0
  • Dipesh Desai  · 技术社区  · 6 年前

    我可以用一个简单的HTML代码来提取它的所有内容。但我只需要使用正则表达式 我已经写了下面的代码,但它有一些错误,我不知道如何解决它。

    function htmlToText(html) {
          return html.
            replace(/(.|\n)*<body.*>/, ''). //remove up till body
            replace(/<\/body(.|\n)*/, ''). //remove from </body
            replace(/<.+\>/, ''). //remove tags
            replace(/^\s\n*$/gm, '');  //remove empty lines
        }
    

    这是解决问题的方法

    function htmlToText(html) {
              return html.
                replace(/(.|\n)*<body.*>/, ''). //remove up till body
                replace(/<\/body(.|\n)*/g, ''). //remove from </body
                replace(/<.+\>/g, ''). //remove tags
                replace(/^\s\n*$/gm, '');  //remove empty lines
            }
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   scniro    6 年前

    不用想太多,你可以 document.body.innerText

    A Sample Document
    Some strong and emphasized text
    

    JSFiddle example