Skip to content Skip to sidebar Skip to footer

Html Page Tags Count Using Regex

I want to parse an entire html page using javascript and count the total number of different tags present in it using regEx and then print it. Can anyone please help on how I go ab

Solution 1:

Description

Count all the tag names in string while avoiding difficult edge cases.

Example

Regular Expression

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

Regular expression visualization

Live Demo

Sample Code

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;
varHashTable = {};

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

    // verify the match was successfulif (m) {
        // verify the HashTable has an entry for the found tag nameif ( !(m[1] inHashTable) ) {
            // no entry was found so we'll add the entry for this tag name and count it as zeroHashTable[m[1]] = 0
        } // end if// increment the tag name counterHashTable[m[1]] ++
    } // end if 
} while (m);

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

Sample Output

<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

Post a Comment for "Html Page Tags Count Using Regex"