[英]how to replace Characters with numbers javascript
i want to replace every Characters with number from 0 to 5 to make var string = "abcdef";
我想用0到5之间的数字替换每个字符,以使
var string = "abcdef";
turn into "01245"
i tried this code but result was not what i want 变成
"01245"
我试过这段代码但结果不是我想要的
var text = "abcdef";
var num =0;
var tx;
var res;
for (var char in text) {
tx = text[char];
res= text.replace(tx,num);
num = num+1;
console.log(res);
}
You can use this 你可以用它
String.prototype.charNumReplace=function(){
var newstr="";
var self=this.toLowerCase();
for(var i =0;i<self.length;i++){
newstr+=self.charCodeAt(i)-97;
}
return newstr;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.