Javascript 获取多个段落标记的内部html。当它们表示为字符串时

Javascript 获取多个段落标记的内部html。当它们表示为字符串时,javascript,node.js,string,algorithm,Javascript,Node.js,String,Algorithm,我需要获取字符串中多个段落元素的内部HTML。 下面是一个输入示例: var HTML = "<p class="Paragraph" >Hello, World 1!</p><p class="Paragraph" >Hello, World 2!</p> 有人知道怎么做吗?下面的代码使用正则表达式来匹配您在问题中描述的段落文本 // A regular expression matching

我需要获取字符串中多个段落元素的内部HTML。 下面是一个输入示例:

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

有人知道怎么做吗?

下面的代码使用正则表达式来匹配您在问题中描述的段落文本

// 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);
//匹配*的正则表达式。
常量正则表达式=/([\s\s]*?)/gm;
const html=`

你好,世界1

你好,世界2

`; 让输出=“”; 让matches=regex.exec(html); //循环,直到不再有匹配项。 while(比赛){ //正则表达式生成一个对象,匹配文本存储在索引1中。请参阅: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions //更多细节。 输出+=匹配[1]; //寻找另一个结果。 matches=regex.exec(html); } 控制台日志(输出);
注:

  • 在正则表达式中,
    [\s\s]
    表示匹配任何字符,包括换行符

a)解析html b)获取
innerText
段落元素是否仅包含文本,或者是否包含其他标记,如Hallo Max很高兴见到您

??或者这不会影响它们是否包含在结果中?@Sascha它们也可以包含div。这对我不起作用,因为在p标记中我有类似于
class=“paration”
的内容。另外,当尝试将其编辑为以下内容时,
const regex=new RegExp(“([\\s\\s]*?)

”,“gm”)
输出是
class=“Pargraph”
您正朝着正确的方向移动,但不需要捕获组(由HTML标记中的
表示)。我会更新答案。@Russoptomus谢谢你的帮助!但是有没有一种方法可以让它与更复杂的HTML一起工作,比如测试2
测试3

这正成为一个不同于您最初要求的问题,对于这种复杂性,我认为我不会使用正则表达式,但可能是一个HTML解析包。@Russoptomus谢谢您的帮助。我最终使用Parse5来解析HTML。
// 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);