Count Times Pattern Is In String

[Total: 0    Average: 0/5]

This shows how to have a form text field entered by the user, and when the user wants (presses button) the javascript will figure out how many times “in” occurs in the text.

Body Code:

<form name=exf1>
<input type="text" name="txt" value="trhin uvininmgin">
<BR><input type=button value="new check" onclick="ck()">

</form>
<script type=text/javascript>
function ck() {
 var _m = "in".toLowerCase(); // pattern to match.
 var _s = document.exf1.txt.value.toLowerCase();
 var _c = 0; // count
 for (var i=0;i<_s.length;i++) {
   if (_m == _s.substr(i,_m.length))
     _c++;
 }
 alert(_c);
}
</script>

Times Viewed: 1

Leave a Reply

Your email address will not be published. Required fields are marked *