var str = '北京人爱北京,北京是中国的首都';
    while(str.indexOf('北京') >= 0)
      str = str.replace('北京',Math.floor(Math.random()*1000));
    alert(str);    

解决方案 »

  1.   

    第一种实现:把字符串拆分,然后拼接
    function done() {
         var str = "北京人爱北京,北京是中国的首都";
         var result = "";
         var arrayOfStrings = str.split("北京");
         for (var i=0; i < arrayOfStrings.length; i++) {
             id = Math.ceil(Math.random()*998)+1;
             if(i == 0)
           result = arrayOfStrings[i];
         else
           result += id + arrayOfStrings[i];
       }
         alert(result);
    }
    第二种实现: 字符串替换
    function done() {
         var str = "北京人爱北京,北京是中国的首都";
         pos = str.indexOf("北京");
      while ( pos != -1 ) {
         id = Math.ceil(Math.random()*998)+1;
         str = str.replace("北京",id);
         pos = str.indexOf("北京");
       }
       alert(str);
    }
      

  2.   

    汗一下楼主,第一种都实现了,第二种不是更简单了?
      var str = '北京人爱北京,北京是中国的首都';
        while(str.indexOf('北京') >= 0)
          str = str.replace('北京','北京' + Math.floor(Math.random()*1000));
        alert(str);    
      

  3.   

    还真是有问题,修改一下就好
    var str = '北京人爱北京,北京是中国的首都';
    while(str.indexOf('北京') >= 0)
      str = str.replace('北京','$$reptext$$' + Math.floor(Math.random()*1000));//这个$$reptext$$是字符串不可能出现的内容
    while(str.indexOf('$$reptext$$') >= 0)
      str = str.replace('$$reptext$$','北京');
    alert(str);    
      

  4.   

    经测试,完全没问题,拿分了,帅帅的宝宝的作品:必定全部代码!
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <script language="JavaScript" type="text/javascript">
    function replace(){
        var str_all = document.getElementById("1").value;
        while(true){
        var x = str_all.search("北京");
        if(x == -1) break;
        var str_all = str_all.substring(0,x) + Math.floor(Math.random( ) * 999 + 1) + str_all.substring(x+2,str_all.length);
        document.getElementById("1").setAttribute("value",str_all);
    }
    }
    </script>
    <title>无标题文档</title>
    </head>
    <body>
    <input size="50" type="text" id="1" value = "北京人爱北京,北京是中国的首都"/>
    <input type="button" value="替换" onclick="replace()">
    </body>
    </html>
      

  5.   

    千万不要用replace函数,因为会一次性替换随机函数,这就出现了替换的东西是一样的。
      

  6.   

    IE 5.5和以上支持replace可以传函数
    方法如下<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <script type="text/javascript">
    function replaceStr(){ 
    with (document.getElementById("1")) value = value.replace(/北京/g, function () {
    return Math.round(Math.random() * 999 + 1);
    });

    </script>
    <title>无标题文档 </title>
    </head>
    <body>
    <input size="50" type="text" id="1" value ="北京人爱北京,北京是中国的首都"/>
    <input type="button" value="替换" onclick="replaceStr()">
    </body>
    </html>