"a11a".replace(/1(?=[^1]*$)/, "2")

解决方案 »

  1.   

    楼上这位星字辈的正则应该没有问题吧。偶对正则是一点都不懂的说:(
    function replaceIt(theStr){
    var replaceStr = "";
    var thePosition = 0;
    var headStr = "";
    var endStr = "";
    var nowReplacestr="";
    var j;
    for(var i = 0; i < theStr.length;i++){
    if(theStr.charAt(i) == "1"){
    replacestr+=theStr.charAt(i);
    thePosition = i;
      }
    else
    if(i <= thePosition){
    headStr+=headStr;
    thePosition+=thePosition;
       }
    else{
    endStr+=endStr;
    thePosition+=thePosition;
       }
     }
    for(var j =0 ;j < replaceStr.length;j++){
    nowReplacestr += replaceStr.charAt(j);
    if(j==replaceStr.length - 1)
    nowReplacestr+="2";
        }
    str=headStr + nowReplacestr + endStr;
    return str;
    }
      

  2.   

    function replaceIt(str){
     var result="";
     var pos=0;
     if((pos=str.lastIndexOf("1"))!=-1){
     
      result=str.substring(0,pos)+"2"+str.substring(pos+1,str.length); }
    return result;
    }