字符串替换问题,不会替换

把字符串“javascript is awesome”替换成“j4v4scr1pt 1s 4w3s0m”
var input="javascript is awesome";
var output="";
for(var i=0;i<input.length;i++){
output+=input[i];
output["a"]=["4"];
output["o"]=["0"];
output["i"]=["1"];
output["e"]=["3"];
}

console.log(output)

6个回答

这个是我朋友帮我解答的,因为我学的很基本的,所以只能看懂这个

 var input="javascript is awesome";
var output="";
for(var i=0;i<input.length;i++){
    if(input[i] == 'a'){
        output += '4';
    }else if(input[i] == 'i'){
        output += '1';
    }else if(input[i] == 'o'){
        output += '0';
    }else if(input[i] == 'e'){
        output += '3';
    }else{
        output += input[i];
    }
}
console.log(output);

    var s = 'javascript is awesome',
        kv = { a: 4, o: 0, i: 1, e: 3 }
    for (attr in kv) s = s.replace(new RegExp(attr, 'gi'), function ($0) { return kv[$0] });
    alert(s)
hehehehehe000
hehehehehe000 回复showbo: 其实从第二行开始我就看的吃力了,因为没学到这些用法。谢谢帮我回答
2 年多之前 回复
showbo
支付宝加好友偷能量挖 回复hehehehehe000: 要区分大小写,改下正则RegExp参数。new RegExp(attr, 'g'),去掉i。记得采纳:)。。
2 年多之前 回复
showbo
支付宝加好友偷能量挖 回复hehehehehe000: 去掉alert就行了,alert只是提示你内容已经修改了而已
2 年多之前 回复
hehehehehe000
hehehehehe000 很厉害,谢谢回答。只是我只改字母,不要对话框的。
2 年多之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 简洁!
2 年多之前 回复

package Ans;
/**

  • @author GeGe
  • @date 2017/7/16
  • **/ public class ReplaceStr{ public static void main(String[] args){ //“javascript is awesome”替换成“j4v4scr1pt 1s 4w3s0m” String str="javascript is awesome".toString(); String ReplaceMentStr=""; // demo1 for(int temp=0;temp<str.length()-1;temp++){ ReplaceMentStr+=str.charAt(temp); if(ReplaceMentStr.contains("a")){ ReplaceMentStr=ReplaceMentStr.replaceAll("a","4"); } if(ReplaceMentStr.contains("e")){ ReplaceMentStr=ReplaceMentStr.replaceAll("e","3"); } if(ReplaceMentStr.contains("i")){ ReplaceMentStr=ReplaceMentStr.replaceAll("i","1"); } if(ReplaceMentStr.contains("o")){ ReplaceMentStr=ReplaceMentStr.replaceAll("o","0"); } } // demo2 str=str.replaceAll("a","4"); str=str.replaceAll("e","3"); str=str.replaceAll("i","1"); str=str.replaceAll("o","0"); str=str.replaceAll("o","0"); String replacement=""; for(int temp=0;temp<str.length()-1;temp++){ replacement+=str.charAt(temp)+""; } //demo3 String strr="javascript is awesome".toString(); String Rstr=""; for(int temp=0;temp<strr.length()-1;temp++){ switch(strr.charAt(temp)){ case 'a': Rstr+='4'; break; case 'e': Rstr+='3'; break; case 'i': Rstr+='1'; break; case 'o': Rstr+='0'; break; default: Rstr+=strr.charAt(temp); } } System.out.println("demo1:"+Rstr); System.out.println("demo2:"+replacement); System.out.println("demo3:"+ReplaceMentStr); } } 运行结果: demo1:j4v4scr1pt 1s 4w3s0m demo2:j4v4scr1pt 1s 4w3s0m demo3:j4v4scr1pt 1s 4w3s0m

package Ans;
/**

  • @author GeGe
  • @date 2017/7/16
  • **/ public class ReplaceStr{ public static void main(String[] args){ //“javascript is awesome”替换成“j4v4scr1pt 1s 4w3s0m” String str="javascript is awesome".toString(); String ReplaceMentStr=""; // demo1 for(int temp=0;temp<str.length()-1;temp++){ ReplaceMentStr+=str.charAt(temp); if(ReplaceMentStr.contains("a")){ ReplaceMentStr=ReplaceMentStr.replaceAll("a","4"); } if(ReplaceMentStr.contains("e")){ ReplaceMentStr=ReplaceMentStr.replaceAll("e","3"); } if(ReplaceMentStr.contains("i")){ ReplaceMentStr=ReplaceMentStr.replaceAll("i","1"); } if(ReplaceMentStr.contains("o")){ ReplaceMentStr=ReplaceMentStr.replaceAll("o","0"); } } // demo2 str=str.replaceAll("a","4"); str=str.replaceAll("e","3"); str=str.replaceAll("i","1"); str=str.replaceAll("o","0"); str=str.replaceAll("o","0"); String replacement=""; for(int temp=0;temp<str.length()-1;temp++){ replacement+=str.charAt(temp)+""; } //demo3 String strr="javascript is awesome".toString(); String Rstr=""; for(int temp=0;temp<strr.length()-1;temp++){ switch(strr.charAt(temp)){ case 'a': Rstr+='4'; break; case 'e': Rstr+='3'; break; case 'i': Rstr+='1'; break; case 'o': Rstr+='0'; break; default: Rstr+=strr.charAt(temp); } } System.out.println("demo1:"+Rstr); System.out.println("demo2:"+replacement); System.out.println("demo3:"+ReplaceMentStr); } } 运行结果:

demo1:j4v4scr1pt 1s 4w3s0m
demo2:j4v4scr1pt 1s 4w3s0m
demo3:j4v4scr1pt 1s 4w3s0m



 var input="javascript is awesome";
var output="";
var find="aiso";
var replacewith = "4130";
for(var i=0;i<input.length;i++){
var b= false;
for (var j=0;j<find.length;j++)
{
if (output[i]==find[j]) { output+= replacewith[j]; b = true; }
}
if (!b) output+=input[i];
}
 var input="javascript is awesome";
var output="";
console.log(input);
for(var i=0;i<input.length;i++)
{
    var c= input[i];
    switch(input[i])
    {
        case 'a':
            c = '4';
            break;
        case 'o':
            c = '0';
            break;
        case 'i':
            c = '1';
            break;
        case 'e':
            c = '3';
            break;
        default:
            break;
    }
    output+=c;
}

console.log(output);
hehehehehe000
hehehehehe000 有部分没看懂,是我没学到的部分。还是很感谢帮忙回答
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问