javascript无法通过innerHTML进行搜索

时间:2016-12-15 03:42:12

标签: javascript greasemonkey innerhtml

我试图在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。

1 个答案:

答案 0 :(得分:1)

您无需转义"引号即可匹配字符串中的转义引号。

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

在实际字符串中,那些转义斜杠实际上并不存在,如果字符串使用该字符作为分隔符,则无法表示文字双(或单)引用,因此您必须将其转义。< / p>

这是一个有效的例子:

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);