简体   繁体   中英

Getting the inner html of multiple paragraph tags. When they are represented as a string

I need to get the inner HTML of multiple paragraph elements that are in a string. Here's an example input:

var HTML = "<p class="Paragraph" >Hello, World 1!</p><p class="Paragraph" >Hello, World 2!</p>

Here's what I want the output to be:

var result = "Hello, World 1!Hello, World 2!"

Does anybody know how to do this?

The following code uses a regular expression to match paragraph text as you describe in the question.

// A regular expression matching <*>*</*>.
const regex = /<[\s\S]*?>([\s\S]*?)<\/[\s\S]*?>/gm;
const html = `<p class="Paragraph" >Hello, World 1!</p><p class="Paragraph" >Hello, World 2!</p>`;

let output = '';
let matches = regex.exec(html);
// Loop until there are no more matches.
while (matches) {
    // Regex produces an object where the matching text is stored at index 1. See: 
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
    // For more details.
    output += matches[1];
    // Look for another result.
    matches = regex.exec(html);
}

console.log(output);

Notes:

  • In the regular expression, [\s\S] means match any character, including newlines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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