简体   繁体   中英

String Decrypting : unmatched ) in regular expression

Here is my code :

String.prototype.escape = function(){
  return this.replace(new RegExp("[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]", "g"), "\\$&");
}

String.prototype.replaceAll = function(search, replace){
    if(!replace){
        return this;
    }

    return this.replace(new RegExp(search, 'g'), replace);
};

String.prototype.RreplaceAll = function(search_string, replace_string){
    if(!replace_string){
        return this;
    }

    return this.replace(new RegExp(replace_string, 'g'), search_string);
};

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

  string.escape();

  var 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/");
  string = string.replaceAll("0", "/)\\");
  string = string.replaceAll("1", "/!\\");
  string = string.replaceAll("2", "/@\\");
  string = string.replaceAll("3", "/#\\");
  string = string.replaceAll("4", "/$\\");
  string = string.replaceAll("5", "/%\\");
  string = string.replaceAll("6", "/^\\");
  string = string.replaceAll("7", "/&\\");
  string = string.replaceAll("8", "/*\\");
  string = string.replaceAll("9", "/(\\");

  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 + "/");
  }

  document.getElementById("text").innerHTML = string;
}

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

  string.escape();

  var 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]);
  }

  string = string.RreplaceAll("/", "53/");
  string = string.RreplaceAll("0", "/)\\");
  string = string.RreplaceAll("1", "/!\\");
  string = string.RreplaceAll("2", "/@\\");
  string = string.RreplaceAll("3", "/#\\");
  string = string.RreplaceAll("4", "/$\\");
  string = string.RreplaceAll("5", "/%\\");
  string = string.RreplaceAll("6", "/^\\");
  string = string.RreplaceAll("7", "/&\\");
  string = string.RreplaceAll("8", "/*\\");
  string = string.RreplaceAll("9", "/(\\");

  document.getElementById("text").innerHTML = string;
}

However when I decrypt() with any string, it gave an error at console :

SyntaxError: unmatched ) in regular expression

What can I do? I've tried doing something to escape the string with special characters, but it still doesn't work. Where's the problem? Please help.

You are trying to use "/)\\\\" as a regular expression, in:

string = string.RreplaceAll("0", "/)\\");

but ) is a special character in regular expressions. You have to escape the character first, manually ( "/\\\\)\\\\" ) or with your .escape method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM