html页面正则表达式,使用正则表达式计算HTML页面标记

描述

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

正则表达式

])(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\s]*)*\s?\/?>

9966a69943a6b8a6ab60e942023da259.png

现场演示

示例代码

var string = "

This is a tagt 2

This is paragraph1

This is Assigntment 2

This is paragraph1

";

console.log(string);

var re = /])(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\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]);

}

样本输出

This is a tagt 2

This is paragraph1

This is Assigntment 2

This is paragraph1

html=1

head=1

body=2

a=2

p=2

div=1

img=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值