最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - loop through html tags in string and add inner text to array - Stack Overflow

programmeradmin9浏览0评论

I have some HTML content saved as a string.

I'd like to loop through each of the header tags in that string and get its inner text.

let str = `<h1>topic 1</h1><p>desc of topic 1</p><h1>topic 2</h1><p>desc of topic 2</p>`;

const innerHTMLarr = str.match(/<h1>(.*?)<\/h1>/g).map(x => x);

console.log(innerHTMLarr)

I have some HTML content saved as a string.

I'd like to loop through each of the header tags in that string and get its inner text.

let str = `<h1>topic 1</h1><p>desc of topic 1</p><h1>topic 2</h1><p>desc of topic 2</p>`;

const innerHTMLarr = str.match(/<h1>(.*?)<\/h1>/g).map(x => x);

console.log(innerHTMLarr)

The array es back with the whole header text, how do I get just the inner text?

Wouldn't mind using jQuery.

Share Improve this question edited May 15, 2018 at 23:02 Mamun 69k9 gold badges51 silver badges62 bronze badges asked May 15, 2018 at 9:27 totalnoobtotalnoob 2,74110 gold badges39 silver badges72 bronze badges 3
  • 1 please refere this stackoverflow./questions/10585029/… – Kalleshwar Kalshetty Commented May 15, 2018 at 9:33
  • Possible duplicate of Javascript global match with capturing groups – NineBerry Commented May 15, 2018 at 9:39
  • See also How do you access the matched groups in a JavaScript regular expression? – NineBerry Commented May 15, 2018 at 9:50
Add a ment  | 

4 Answers 4

Reset to default 5

Try /<\/?h1>/g inside map() to replace all occurrences of <h1> and <\h1> with '' like the following:

let str = `<h1>topic 1</h1><p>desc of topic 1</p><h1>topic 2</h1><p>desc of topic 2</p>`;

const innerHTMLarr = str.match(/<h1>(.*?)<\/h1>/g).map(val => {
   return val.replace(/<\/?h1>/g,'');
});
console.log(innerHTMLarr)

Using jQuery, you can do it the following way:

    let str = '<h1>topic 1</h1><p>desc of topic 1</p><h1>topic 2</h1><p>desc of topic 2</p>';
    html = $.parseHTML( str );
    innerHTMLarr = [], k=0;
    $.each( html, function( i, el ) {
    	if(el.nodeName.startsWith('H')) 
        	innerHTMLarr[k++] = el.innerHTML;
    });    
    console.log(innerHTMLarr);
<script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>

You can use exec() in a loop until there is no match.

EDIT : reduced code

let pattern = /<h1>(.*?)<\/h1>/g;
let str = `<h1>topic 1</h1><p>desc of topic 1</p><h1>topic 2</h1><p>desc of topic 2</p>`;

let match;

while (match = pattern.exec(str))
    console.log(match[1]);

Applying the solution from Javascript global match with capturing groups:

let str = `<h1>topic 1</h1><p>desc of topic 1</p><h1>topic 2</h1><p>desc of topic 2</p>`;

let regexpr = /<h1>(.*?)<\/h1>/g;

let match = regexpr.exec(str);

while(match !== null) {
    console.log(match[1]);
    match = regexpr.exec(str);
}

发布评论

评论列表(0)

  1. 暂无评论