为什么此代码返回错误警报? [英] Why does this code returns wrong alert?

查看:92
本文介绍了为什么此代码返回错误警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:



 字符串 .prototype.replaceAll = function (搜索,替换){
if (!replace){
return < span class =code-keyword> this ;
}
返回 .replace( new RegExp(' [' + search + ' ]'' g'),替换);
};

函数encrypt(){
var string = prompt( 要加密的字符串:);

replace_array = [ a b c d e f g h i,< span class =code-string> j k l m n o p q r s t u v w x y z B C D E F G H < span class =code-string> J, K L M N O P Q R S T U V W X Y Z];

string = string .replaceAll( / 53 /);

for var i = 0 ; i < 52 ; i ++){
if (i < 9 ){
new_string = 0 + String (i + 1 );
} else {
new_string = String (i + 1 );
}

string = string .replaceAll(replace_array [i ],new_string + /);
}

alert( string );
}

函数decrypt(){
var string = prompt( 要解密的字符串:);

replace_array = [ a b c d e f g h i,< span class =code-string>
j k l m n o p q r s t u v w x y z B C D E F G H < span class =code-string> J
K L M N O P Q R S T U V W X Y Z];

for var i = 0 ; i < 52 ; i ++){
if (i < 9 ){
old_string = 0 + 字符串 (i + 1 );
} else {
old_string = String (i + 1 );
}

old_string = old_string + /;

string = string .replaceAll(old_string,replace_array [i]);
}

alert( string );
}







当我

 decrypt()

string`01 / 02/03 /`我想得到`abc`而不是aaaabaaca。



我该怎么办?

解决方案

将replaceAll更改为替换:

 string = string.replace(old_string,replace_array [i]); 


Code :

String.prototype.replaceAll = function(search, replace){
    if(!replace){
        return this;
    }
    return this.replace(new RegExp('[' + search + ']', 'g'), replace);
};

function encrypt(){
  var string = prompt("String to Encrypt : ");

  replace_array = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];

  string = string.replaceAll("/", "53/");

  for (var i = 0; i < 52; i++){
    if (i < 9) {
      new_string = "0" + String(i + 1);
    } else {
      new_string = String(i + 1);
    }

    string = string.replaceAll(replace_array[i], new_string + "/");
  }

  alert(string);
}

function decrypt(){
  var string = prompt("String to Decrypt : ");

  replace_array = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];

  for (var i = 0; i < 52; i++){
    if (i < 9) {
      old_string = "0" + String(i + 1);
    } else {
      old_string = String(i + 1);
    }

    old_string = old_string + "/";

    string = string.replaceAll(old_string, replace_array[i]);
  }

  alert(string);
}




When I

decrypt()

string `01/02/03/` I want to get `abc` instead of aaaabaaca.

What can I do?

解决方案

Change replaceAll to replace:

string = string.replace(old_string, replace_array[i]);


这篇关于为什么此代码返回错误警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆