描述
计算字符串中的所有标签名称,同时避免困难的边缘情况。
例
正则表达式
])(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\s]*)*\s?\/?>
现场演示
示例代码
var string = "
This is a tagt 2This is paragraph1
This is Assigntment 2This 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 2This is paragraph1
This is Assigntment 2This is paragraph1
html=1
head=1
body=2
a=2
p=2
div=1
img=1