<script>
function c(str,key){
try{return str.match(new RegExp(key,"g")).length}catch(e){return 0}
}
alert(c("abcaaabaaabcabc","aaa"))
alert(c("abcaabaabaabcabc","aaa"))
</script>

解决方案 »

  1.   

    为什么js里面居然有try...catch结构?
      

  2.   

    try...catch...finally 语句
    为 JScript 实现错误处理。 try {
       tryStatements}
    catch(exception){
       catchStatements}
    finally {
      finallyStatements}参数
    tryStatement必选项。可能发生错误的语句。exception可选项。任何变量名。exception 的初始化值是扔出的错误的值。catchStatement可选项。处理在相关联的 tryStatement 中发生的错误的语句。finallyStatements可选项。在所有其他过程发生之后无条件执行的语句。
      

  3.   

    match 方法
    使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回。
    stringObj.match(rgExp) RegExp 对象
    保存有关正则表达式模式匹配信息的固有全局对象。function MatchDemo(){
       var r, re;                     // 声明变量。
       var s = "The rain in Spain falls mainly in the plain";
       re = new RegExp("Spain","i");  // 创建正则表达式对象。
       r = s.match(re);               // 在字符串 s 中查找匹配。
       return(r);                     // 返回匹配结果。
    }详情参考:
    微软脚本帮助文档5.6
    含jscript、vbscript等http://download.microsoft.com/download/winscript56/Install/5.6/W982KMe/CN/scd56chs.exe