Saturday, 21 July 2012

count occurrence of word in sentences JavaScript


       var temp = "This is a string.";

        // the g in the regular expression says to search the whole string
        // rather than just find the first occurrence
        var count = temp.match(/is/g);

        alert(count.length);
       
          (Or)
          alert(temp .split("is").length - 1);


       Result:
       1





No comments:

Post a Comment