<script type="text/javascript"> 
var count=0;
var str="Welcome to Microsoft! " 
str=str + "We are proud to announce that Microsoft has " 
str=str + "one of the largest Web Developers sites in the world." 
var reg=/Microsoft/g;while (reg.exec(str)!=null)
{
  count++;
}alert("总计匹配的个数有:"+count);//document.write(str.replace(/Microsoft/g, "微軟")) </script>

解决方案 »

  1.   

    <script type="text/javascript"> var str="Welcome to Microsoft! " 
    str=str + "We are proud to announce that Microsoft has " 
    str=str + "one of the largest Web Developers sites in the world." var s="Microsoft";
    alert(eval("str.match(/"+s+"/ig).length"))//个数document.write(str.replace(/Microsoft/g, "微軟")) </script> 
      

  2.   

            var str="Welcome to Microsoft! " 
    str=str + "We are proud to announce that Microsoft has " 
    str=str + "one of the largest Web Developers sites in the world."
    var number=0; 
    while(str.indexOf("Microsoft")!=-1){
    alert(str.indexOf("Microsoft"));
        str=str.replace("Microsoft", "微軟");
        number++;
        alert(number);
    }