我想使用 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
标签的数量 = 2p
标签数 = 2- 等等
最佳答案
描述
计算字符串中的所有标签名称,同时避免困难的边缘情况。
例子
正则表达式
<([a-z]+)(?=[\s>])(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\s]*)*\s?\/?>
现场演示
- 正则表达式:https://regex101.com/r/mA4hF0/1
- Javascript:https://repl.it/C0gY/1
示例代码
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/