Skip to content

Instantly share code, notes, and snippets.

@SIRHAMY
Created May 29, 2018 22:11
Show Gist options
  • Save SIRHAMY/2d3063eb1019aa832b5f352ad9dfb78c to your computer and use it in GitHub Desktop.
Save SIRHAMY/2d3063eb1019aa832b5f352ad9dfb78c to your computer and use it in GitHub Desktop.
Count the number of occurrences of a Regex match on a given webpage.
var minNumberOfOccurrencesToPrint = 3; // Min number of occurrences you care about
var documentHtml = document.documentElement.outerHTML; // Grab HTML
var occurrences = (documentHtml.match(/YourRegexGoesHere/g) || []); // Search page for occurrences, use regexpal.com to test your regex
var wordCounter = {};
for (let match of occurrences) {
if(match in wordCounter) {
wordCounter[match] += 1;
} else {
wordCounter[match] = 1;
}
}
for (const[key, value] of Object.entries(wordCounter)) {
if(value >= minNumberOfOccurrencesToPrint) {
console.log(key + ": " + value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment