我正在尝试替换元素的html代码。它看起来像是:
<a class="disp_img" onclick="increase('\x6c\x6f\x61\x64\x5f\x6d\x65\x6e\x75\x28\x24\x28\x27\x23\x69\x6d\x61\x67\x65\x73\x27\x29\x2e\x68\x74\x6d\x6c\x28\x29\x2c\x20\x24\x28\x27\x23\x69\x6d\x61\x67\x65\x73\x5f\x32\x27\x29\x2e\x68\x74\x6d\x6c\x28\x29\x2c\x20\x27\x72\x61\x6e\x64\x6f\x6d\x27\x2c\x20\x27\x73\x74\x72\x69\x63\x74\x27\x29');" href="javascript:void(0);">Display Images</a>
这是:
'load_menu($('#images').html(), $('#images_2').html(), 'random', 'strict')'
但它需要保持HEX。
我只想将“随机”替换为“严格”,如下所示:
var found = [];
var changed = [];
var c = 0;
var searchText = "Display Images";
var as = document.getElementsByClassName("disp_img");
for (var i = 0; i < as.length; i++) {
if (as[i].innerText == searchText){
found[c] = as[i].outerHTML;
c++;
}
}
for (i = 0; i < found.length; i++) {
changed[i] = found[i].toString();
changed[i] = changed[i].replace("\x27\x72\x61\x6e\x64\x6f\x6d\x27\x2c\x20\x27\x73\x74\x72\x69\x63\x74\x27", "\x27\x73\x74\x72\x69\x63\x74\x27\x2c\x20\x27\x73\x74\x72\x69\x63\x74\x27");
console.log(changed[i]);
}
我可以管理其余部分,但我遇到了这个问题。
如何正确使用replace
方法替换该字符串?
答案 0 :(得分:0)
好的,我明白了。我需要加倍反斜杠,比如.replace("\\x27\\x72\\x61
等。