使用JavaScript从HTML字符串中提取文本

时间:2022-09-13 09:52:45

I am trying to get the inner text of HTML string, using a JS function(the string is passed as an argument). Here is the code:

我试图使用JS函数获取HTML字符串的内部文本(字符串作为参数传递)。这是代码:

function extractContent(value) {
    var content_holder = "";

    for(var i=0;i<value.length;i++) {
        if(value.charAt(i) === '>') {
            continue;
            while(value.charAt(i) != '<') {
                content_holder += value.charAt(i);
            }
        }

    }
    console.log(content_holder);
}

extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>");

The problem is that nothing gets printed on the console(content_holder stays empty). I think the problem is caused by the "===" operator..

问题是控制台上没有打印任何内容(content_holder保持空白)。我认为问题是由“===”运算符引起的..

6 个解决方案

#1


23  

Create an element, store the HTML in it, and get its textContent:

创建一个元素,将HTML存储在其中,并获取其textContent:

function extractContent(s) {
  var span = document.createElement('span');
  span.innerHTML = s;
  return span.textContent || span.innerText;
};
    
alert(extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>"));


Here's a version that allows you to have spaces between nodes, although you'd probably want that for block-level elements only:

这是一个允许您在节点之间有空格的版本,尽管您可能只想要块级元素:

function extractContent(s, space) {
  var span= document.createElement('span');
  span.innerHTML= s;
  if(space) {
    var children= span.querySelectorAll('*');
    for(var i = 0 ; i < children.length ; i++) {
      if(children[i].textContent)
        children[i].textContent+= ' ';
      else
        children[i].innerText+= ' ';
    }
  }
  return [span.textContent || span.innerText].toString().replace(/ +/g,' ');
};
    
console.log(extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>.  Nice to <em>see</em><strong><em>you!</em></strong>"));

console.log(extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>.  Nice to <em>see</em><strong><em>you!</em></strong>",true));

#2


14  

One line (more precisely, one statement) version:

一行(更确切地说,一个声明)版本:

function extractContent(html) {

    return (new DOMParser).parseFromString(html, "text/html") . 
        documentElement . textContent;

}

#3


2  

use this regax for remove html tags and store only the inner text in html

使用此regax删除html标记并仅在html中存储内部文本

it shows the HelloW3c only check it

它显示HelloW3c只检查它

var content_holder = value.replace(/<(?:.|\n)*?>/gm, '');

#4


1  

Try This:-

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function extractContent(value){
        var div = document.createElement('div')
        div.innerHTML=value;
        var text= div.textContent;            
        return text;
}
window.onload=function()
{
   alert(extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>"));
};
</script>
</body>
</html>

#5


-1  

You could temporarily write it out to a block level element that is positioned off the page .. some thing like this:

你可以暂时将它写出一个位于页面外的块级元素..这样的事情:

HTML:

<div id="tmp" style="position:absolute;top:-400px;left:-400px;">
</div>

JavaScript:

<script type="text/javascript">
function extractContent(value){
        var div=document.getElementById('tmp');
        div.innerHTML=value;
        console.log(div.children[0].innerHTML);//console out p
}

extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>");
</script>

#6


-3  

you need array to hold values

你需要数组来保存值

  function extractContent(value) {
var content_holder = new Array();

for(var i=0;i<value.length;i++) {
    if(value.charAt(i) === '>') {
        continue;
        while(value.charAt(i) != '<') {
            content_holder.push(value.charAt(i));
            console.log(content_holder[i]);
        }
    }
}
}extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>");

#1


23  

Create an element, store the HTML in it, and get its textContent:

创建一个元素,将HTML存储在其中,并获取其textContent:

function extractContent(s) {
  var span = document.createElement('span');
  span.innerHTML = s;
  return span.textContent || span.innerText;
};
    
alert(extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>"));


Here's a version that allows you to have spaces between nodes, although you'd probably want that for block-level elements only:

这是一个允许您在节点之间有空格的版本,尽管您可能只想要块级元素:

function extractContent(s, space) {
  var span= document.createElement('span');
  span.innerHTML= s;
  if(space) {
    var children= span.querySelectorAll('*');
    for(var i = 0 ; i < children.length ; i++) {
      if(children[i].textContent)
        children[i].textContent+= ' ';
      else
        children[i].innerText+= ' ';
    }
  }
  return [span.textContent || span.innerText].toString().replace(/ +/g,' ');
};
    
console.log(extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>.  Nice to <em>see</em><strong><em>you!</em></strong>"));

console.log(extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>.  Nice to <em>see</em><strong><em>you!</em></strong>",true));

#2


14  

One line (more precisely, one statement) version:

一行(更确切地说,一个声明)版本:

function extractContent(html) {

    return (new DOMParser).parseFromString(html, "text/html") . 
        documentElement . textContent;

}

#3


2  

use this regax for remove html tags and store only the inner text in html

使用此regax删除html标记并仅在html中存储内部文本

it shows the HelloW3c only check it

它显示HelloW3c只检查它

var content_holder = value.replace(/<(?:.|\n)*?>/gm, '');

#4


1  

Try This:-

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function extractContent(value){
        var div = document.createElement('div')
        div.innerHTML=value;
        var text= div.textContent;            
        return text;
}
window.onload=function()
{
   alert(extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>"));
};
</script>
</body>
</html>

#5


-1  

You could temporarily write it out to a block level element that is positioned off the page .. some thing like this:

你可以暂时将它写出一个位于页面外的块级元素..这样的事情:

HTML:

<div id="tmp" style="position:absolute;top:-400px;left:-400px;">
</div>

JavaScript:

<script type="text/javascript">
function extractContent(value){
        var div=document.getElementById('tmp');
        div.innerHTML=value;
        console.log(div.children[0].innerHTML);//console out p
}

extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>");
</script>

#6


-3  

you need array to hold values

你需要数组来保存值

  function extractContent(value) {
var content_holder = new Array();

for(var i=0;i<value.length;i++) {
    if(value.charAt(i) === '>') {
        continue;
        while(value.charAt(i) != '<') {
            content_holder.push(value.charAt(i));
            console.log(content_holder[i]);
        }
    }
}
}extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>");