这是我的代码:
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;
}
但是当我decrypt()
使用任何字符串时,它在控制台上出错:
SyntaxError: unmatched ) in regular expression
我该怎么办?我尝试过用特殊字符来逃避字符串,但它仍然无法正常工作。问题出在哪里?请帮忙。
答案 0 :(得分:1)
您正尝试将"/)\\"
用作正则表达式:
string = string.RreplaceAll("0", "/)\\");
但)
是正则表达式中的特殊字符。您必须先手动("/\\)\\"
)或使用.escape
方法转义角色。