Created
May 29, 2018 22:11
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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