700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > html页面正则表达式 使用正则表达式计算HTML页面标记

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

时间:2024-02-25 02:43:54

相关推荐

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

描述

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

正则表达式

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

现场演示

示例代码

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。