javascript - Html 页面标记使用正则表达式计数

标签 javascript regex node.js

我想使用 javascript 解析整个 html 页面,并使用 regEx 计算其中存在的不同标签的总数,然后打印它。任何人都可以帮助我如何去做吗?代码将非常受欢迎

例如,如果这是 html 页面:

<html> <head> </head> <body> <a>This is a tagt 2</a> <p>This is 
paragraph1</p> <a>This is Assigntment 2</a> <p>This is paragraph1
</p> <div> <img> </img> </div> <body> </html> 

那么预期的输出是:

  • a 标签的数量 = 2
  • p 标签数 = 2
  • 等等

最佳答案

描述

计算字符串中的所有标签名称,同时避免困难的边缘情况。

例子

正则表达式

<([a-z]+)(?=[\s>])(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\s]*)*\s?\/?>

Regular expression visualization

现场演示

示例代码

var string = "<html> <head> </head> <body> <a>This is a tagt 2</a> <p>This is paragraph1</p> <a>This is Assigntment 2</a> <p>This is paragraph1</p> <div> <img> </img> </div> <body> </html>";

console.log(string);
var re = /<([a-z]+)(?=[\s>])(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\s]*)*\s?\/?>/gi;
var m;
var HashTable = {};

do {
    // conduct the match 
    m = re.exec(string);

    // verify the match was successful
    if (m) {
        // verify the HashTable has an entry for the found tag name
        if ( !(m[1] in HashTable) ) {
            // no entry was found so we'll add the entry for this tag name and count it as zero
            HashTable[m[1]] = 0
        } // end if

        // increment the tag name counter
        HashTable[m[1]] ++
    } // end if 
} while (m);

console.log("")
// output the number of all found tag names
for (var key in HashTable) {
    console.log(key + "=" + HashTable[key]);
}

示例输出

<html> <head> </head> <body> <a>This is a tagt 2</a> <p>This is paragraph1</p> <a>This is Assigntment 2</a> <p>This is paragraph1</p> <div> <img> </img> </div> <body> </html>

html=1
head=1
body=2
a=2
p=2
div=1
img=1

关于javascript - Html 页面标记使用正则表达式计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37740880/

相关文章:

javascript - 如何使用 feather js 更新 html 文档?

node.js - 从 Node.js 中的命令行从 Google Drive 获取文件?

node.js - nodejs在mongodb中获取查找结果

python - 正则表达式数字之间的整个字符串匹配

java - 否定复杂的正则表达式

javascript - 过滤字符串中找到的所有主题标签

javascript - 如何在 Javascript 中更改 html 中动态 div 标签的 div 样式宽度?

javascript - 如何在空动态树上删除节点

javascript - 根据选定的样式表在页面之间导航时切换链接文本

javascript - 如何在 Handlebars 中显示 json 数组中的数据?