gpt4 book ai didi

javascript 无法通过innerHTML 进行搜索

转载 作者:行者123 更新时间:2023-12-03 05:25:33 25 4
gpt4 key购买 nike

我试图在innerhtml中找到两种不同的文本类型,不知何故它什么也没找到,但是当我使用相同的函数和测试示例对其进行测试时,它可以工作。

    function numberoftext (a){if (a.match(/class=\"chattext\"/g)!==null){return a.match(/class=\"chattext\"/g).length;}else{return 0;}}
function numberofglobal (a){if (a.match(/class=\"chattextglobal\"/g)!==null){return a.match(/class=\"chattextglobal\"/g).length;}else{return 0;}}
function numberofclan (a){if (a.match(/class=\"chattextclan\"/g)!==null){return a.match(/class=\"chattextclan\"/g).length;}else{return 0;}}
function numberofgroup (a){if (a.match(/class=\"chattextgroup\"/g)!==null){return a.match(/class=\"chattextgroup\"/g).length;}else{return 0;}}
function numberofwisper (a){if (a.match(/class=\"chattextwhisper\"/g)!==null){return a.match(/class=\"chattextwhisper\"/g).length;}else{return 0;}}
function numberofworld (a){if (a.match(/class=\"worldsay\"/g)!==null){return a.match(/class=\"worldsay\">/g).length;}else{return 0;}}
function numberofscream (a){if (a.match(/class=\"chattextscream\"/g)!==null){return a.match(/class=\"chattextscream\"/g).length;}else{return 0;}}
var innertextraw1 =chatupdateFrame.document.documentElement.innerHTML;
var innertextraw= innertextraw1.substring(innertextraw1.indexOf("parent.chattextFrame.add(")+26, innertextraw1.indexOf("', 0);"));

console.log("got update",innertextraw);
console.log("t:",numberoftext(innertextraw),"c:",numberofclan(innertextraw),"w:",numberofwisper(innertextraw),"gr:",numberofgroup(innertextraw),"gl:",numberofglobal(innertextraw),"sc:",numberofscream(innertextraw));

innertextraw 的一个示例是:"<p class=\"chattext\"><i><b>noone</b> goes with <b>someone</b> to the house</i></p>"
that 一样测试它当我将 innertextraw 设置为从控制台日志中获取的示例时,效果也很好,在网站中它仅返回 0。

最佳答案

您不需要转义 " 引号来匹配字符串中转义的引号。

a.match(/class="chattext"/g)

在真正的字符串中,这些转义斜杠实际上并不存在,如果字符串使用该字符作为分隔符,则无法表示文字双(或单)引号,因此您必须对其进行转义。

这是一个工作示例:

var innerRawText = "<p class=\"chattext\"><i><b>noone</b> goes with <b>someone</b> to the house</i></p>";

var result = innerRawText.match(/class="chattext"/g);
console.log(result);

关于javascript 无法通过innerHTML 进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41156080/

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