如何在 javascript 中将带连接的字符串转换为真实字符串?
How to convert string with concatenation to real string in javascript?
我在页面响应中有一个看起来像这样的字符串(保存为自动回复):
... hexMD5('2' + '****' + '553545422425744[=11=]5'); ...
为了捕捉这个,我使用:
var hex = autoResponse.split('hexMD5(')[1].split(')')[0];
这现在给了我这个字符串:
'2' + '****' + '553545422425744[=13=]5'
如果我直接把它放到hexMD5()
方法中,它认为'
、+
符号和白色space是秘密的一部分。
我尝试使用 replace()
删除它们,如下所示:
while(hex.split("'").length !== 1) hex = hex.replace("'", "");
while(hex.split("+").length !== 1) hex = hex.replace("+", "");
while(hex.split(" ").length !== 1) hex = hex.replace(" ", "");
但是,当我执行 hexMD5(hex)
时,它给了我一个不正确的十六进制。无论如何我可以将十六进制转换为字符串,它将字符串组合在一起,就像我硬编码一样
hexMD5('2' + '****' + '553545422425744[=15=]5');
如有任何帮助,我们将不胜感激。
您可以为此使用一个简单得多的正则表达式:
hex = hex.replace(/' ?\+ ?'/g, '');
也就是说 "replace all single-quotes, followed by possibly a space, then a plus, then possibly another space, followed by another single quote" 并用空替换那些匹配项,从而删除它们。 (您需要在 + 之前加上 \,因为 + 是 RegEx 中需要转义的特殊字符。)
我在页面响应中有一个看起来像这样的字符串(保存为自动回复):
... hexMD5('2' + '****' + '553545422425744[=11=]5'); ...
为了捕捉这个,我使用:
var hex = autoResponse.split('hexMD5(')[1].split(')')[0];
这现在给了我这个字符串:
'2' + '****' + '553545422425744[=13=]5'
如果我直接把它放到hexMD5()
方法中,它认为'
、+
符号和白色space是秘密的一部分。
我尝试使用 replace()
删除它们,如下所示:
while(hex.split("'").length !== 1) hex = hex.replace("'", "");
while(hex.split("+").length !== 1) hex = hex.replace("+", "");
while(hex.split(" ").length !== 1) hex = hex.replace(" ", "");
但是,当我执行 hexMD5(hex)
时,它给了我一个不正确的十六进制。无论如何我可以将十六进制转换为字符串,它将字符串组合在一起,就像我硬编码一样
hexMD5('2' + '****' + '553545422425744[=15=]5');
如有任何帮助,我们将不胜感激。
您可以为此使用一个简单得多的正则表达式:
hex = hex.replace(/' ?\+ ?'/g, '');
也就是说 "replace all single-quotes, followed by possibly a space, then a plus, then possibly another space, followed by another single quote" 并用空替换那些匹配项,从而删除它们。 (您需要在 + 之前加上 \,因为 + 是 RegEx 中需要转义的特殊字符。)