0

Is there a way i can find number of occurrence of a value inside the DOM in javascript or jQuery?

Like for instance if i have a

var x = "myFunc('hello');"

how to count number of occurrences of "myFunc('hello');" in the whole DOM?

  • What would `indexOf(x)` return? – showdev Oct 31 '18 at 19:12
  • 1
    `.indexOf(x)` for the whole document to count em? but, an indexOf function is to find indexes not to count. – Ele Oct 31 '18 at 19:12
  • "*As if i used the browser find basically.*" - see https://stackoverflow.com/q/8080217/1048572 and https://developer.mozilla.org/en-US/docs/Web/API/Window/find for that. But it doesn't return a count. – Bergi Oct 31 '18 at 19:19
  • @showdev doesn't indexOf work with a count parameter as > -1 to count if exists for instance? can't it be used as > 1 to check if 2 or more exist? –  Oct 31 '18 at 19:37
  • What are you counting? Variables? – showdev Oct 31 '18 at 19:39
  • @showdev Number of times "myFunc('hello');" appears in the DOM, as question states –  Oct 31 '18 at 19:42
  • Use a regular expression match with the `g` modifier. This returns an array of all the matches. Get the length of the array. – Barmar Oct 31 '18 at 19:54
  • I edited the post hope it's more clear now. @Barmar right, i used that before as .replace(//g,''); how can i apply it to the whole document though? –  Oct 31 '18 at 19:58

1 Answers1

1

You can use a regexp match, and get the length of the array of all matches.

var matches = document.body.innerHTML.match(/myFunc\('hello'\)/g);
var count = matches ? matches.length : 0;

Doing it with a variable:

var myVar = "myFunc\\('hello'\\)";
var re = new RegExp(myVar, "g");
var matches = document.body.innerHTML.match(re);
var count = matches ? matches.length : 0;

Note that you need to escape the backslash in myVar so that it will be passed literally to the RegExp constructor.

See Escape a variable within a Regular Expression for a function that can be used to escape all the special characters in a string before using it as a regexp.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks i'll test this out, how can i change 'hello' inside the parenthesis with a variable? –  Oct 31 '18 at 20:08
  • Nevermind i found it in another post, i'll try now and let you know –  Oct 31 '18 at 20:15
  • I'm trying with: var re = new RegExp(myVar,"g"); var matches = document.body.innerText.match(re).length; but it says cannot read length of null –  Oct 31 '18 at 20:21
  • `match()` returns `null` if there are no matches. I've added code to check for that. – Barmar Oct 31 '18 at 20:22
  • Is this supposed to find em also if they're inside onclick=""? the myFunc('var'); is inside an onclick="" attribute of different . Because right now is returning me 0, but it appears twice in the dom –  Oct 31 '18 at 20:29
  • I used `innerText`, so it doesn't look inside HTML tag attributes. I changed it to `innerHTML`, now it looks everywhere. – Barmar Oct 31 '18 at 20:41
  • If i write as you mention above it works, if i try to use my var instead of the given myFunc('hello') in the regex, as explained in this answer https://stackoverflow.com/questions/494035/how-do-you-use-a-variable-in-a-regular-expression it doesn't work. I'm doing: var re = new RegExp(myVar,"g"); var matches = document.body.innerText.match(re); var count = matches ? matches.length : 0; Anyway this is a correct answer, it works as per my question, thank you. –  Oct 31 '18 at 21:02
  • I suspect you're not setting `myVar` correctly. I've updated the answer to show how to do this. The important thing is escaping the backslash. – Barmar Oct 31 '18 at 21:05
  • Yea i think is because i'm passing the var as a string without any \\, so the var content is like: myFunction('23bhjda8s'); (shouldn't "re" be inside .match() instead of myVar in your answer?) thanks btw this put me in the right direction, i'll now check how to format the string with the correct slashes –  Oct 31 '18 at 21:11
  • See https://stackoverflow.com/questions/9621825/escape-a-variable-within-a-regular-expression for a function that will add all necessary escapes. – Barmar Oct 31 '18 at 21:13
  • Thank you very much, i used replace to make opening ( and closing ) as "\\(" and "\\)" and now works! cheers –  Oct 31 '18 at 21:26