1

I have a JSON report that I append to my webpage that monitors a server's processes and every processes that is running it returns the string "runn1ng".

I want to do a summary at the top of the webpage that says:

___ out of 10 processes are running.

The ___ number will be the dynamic number that is counted by JavaScript to see how many "runn1ng" appear on the webpage.

I have seen some documentation on here on how to count occurrences of a string within an array, but I am curious if there is a way to count the string occurrence on the entire webpage.

Lasagna Cat
  • 297
  • 3
  • 24

2 Answers2

2

Something like this should work:

var documentHtml = document.documentElement.outerHTML; // Gets page's HTML
var occurences = (documentHtml.match(/runn1ng/g) || []).length; // Searches the page for occurrences

However, as others have mentioned, it's most likely a better and more maintainable solution to have the JSON expose the number of running processes.

How to count string occurrence in string?

How to get the entire document HTML as a string?

Steven Goodman
  • 576
  • 3
  • 15
0

Try this -

var html = document.documentElement.innerHTML;
var n = html.search("runn1ng");
koolkat
  • 706
  • 3
  • 8
  • 23