gpt4 book ai didi

javascript - 字符串解密: unmatched ) in regular expression

转载 作者:行者123 更新时间:2023-11-28 01:18:17 25 4
gpt4 key购买 nike

这是我的代码:

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

我能做什么?我尝试做一些事情来转义带有特殊字符的字符串,但它仍然不起作用。问题出在哪里?请帮忙。

最佳答案

您正在尝试使用 "/)\\" 作为正则表达式,位于:

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

but ) 是正则表达式中的特殊字符。您必须首先手动转义字符 ("/\\)\\") 或使用 .escape 方法。

关于javascript - 字符串解密: unmatched ) in regular expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23560983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com