js查找一字符重复出现的次数.

解决方案 »

  1.   

    <script>
    function MatchDemo(){
       var r, re;         // 声明变量。
       var s = "The rain in Spain falls mainly in the plain";
       re = /ain/ig;      // 创建正则表达式模式。
       r = s.match(re);   // 尝试去匹配搜索字符串。
       return(r);   // 返回的数组包含了所有 "ain" 
                          // 出现的四个匹配。
    }
    alert(MatchDemo().length)
    </script>
      

  2.   

    <script type="text/javascript">
    //<![CDATA[
     onload=function(){
      var str=document.body.innerHTML.toLowerCase();
      
      alert(fStrGetTimes(str, 'scope'));
      alert(fStrGetTimes(str, 'class'));
     }
     
     function fStrGetTimes(sSrc, sMatch){
      var iTime=0;
      switch(arguments.length){
       case 2:
        var re=new RegExp('\\b('+sMatch+')\\b', 'gi');
        sSrc.replace(re, function($0,$1){
         if($1!='')iTime+=1;
        });
        
        return iTime;
        break;
       case 1:
       default:return 0;
      }
     } // end function fStrGetTimes // shawl.qiu code
    //]]>
    </script>
    <xmp>Class DefinitionsClasses are defined using the class keyword.
    ClassDefinition  class Identifier Inheritance Block
    Inheritance 
       &#171;empty&#187;
    |  extends TypeExpressionallowInLike other definitions, a class definition may be preceded by one or more attributes, which affect the class’s scope, namespace, and semantics. Every class is also a value and has type Type.A class definition may only be located at a scope that allows class definitions, defined as follows:
    The global scope allows class definitions
    A package scope allows class definitions
    A class scope allows class definitions
    If a scope X allows class definitions and a block B is directly inside scope X, then B’s scope also allows class definitionsAccording to these rules, a class may not be defined inside a function or a compound statement other than a block. If a class B is defined as a member of another class A, then B must be declared static.</xmp>
      

  3.   

    这样写比较灵活点...<script type="text/javascript">
    //<![CDATA[
     onload=function(){
      var str=document.body.innerHTML.toLowerCase();
      
      alert(fStrGetTimes(str, 's', true));
      alert(fStrGetTimes(str, 'class'));
     }
     
     function fStrGetTimes(sSrc, sMatch, bCharacter){
      var iTime=0;
      switch(arguments.length){
       case 2:
        var re=new RegExp('\\b('+sMatch+')\\b', 'gi');
        sSrc.replace(re, function($0,$1){
         if($1!='')iTime+=1;
        });
        return iTime;
       
       case 3: 
        var re=new RegExp('('+sMatch+')', 'gi');
        sSrc.replace(re, function($0,$1){
         if($1!='')iTime+=1;
        });
        return iTime;
        
       case 1:
       default:return 0;
      }
     } // end function fStrGetTimes // shawl.qiu code
    //]]>
    </script>
      

  4.   

    function occurs(souce, target) {
      return Souce.split(target).length - 1;
    }alert(occurs('sasawdassaae', 'a'));
      

  5.   

    xjdawu(无法界定)的正则比较标准,xuzuning(唠叨)的split真是别出心裁! ^_^