PDA

View Full Version : Counting occurences of an inputted letter


florida
05-23-2003, 04:21 PM
I am trying to count every occurences of a specific letter called "e" that is entered.

This just prints out the number 1 even though I have entered more occurences of the letter e.

Please advise:


function myfunction()
{
j = document.mypage.myfield.value;
ct = 0;

if(/e/.test(j))
{
ct++;
}
alert(ct);
}

beetle
05-23-2003, 05:05 PM
var someStr = "eaeaea";
var es = someStr.match( /e/g );
alert( es.length );

cheesebagpipe
05-23-2003, 06:33 PM
String.prototype.occurs = function(chr) {
var temp = this.match(new RegExp(chr,'g'));
if (temp != null)
return temp.length;
else return 0;
}

var someStr = "aeiou";
alert(someStr.occurs('e'));
alert(someStr.occurs('z'));

florida
05-24-2003, 03:23 AM
Thank you!