htmlparser2是一个fast&forgivigHTML/XML解析器。
pmistallhtmlparser2htmlparser2的 livedemo:https://astexplorer.et/#/2AmVrGuGVJ
htmlparser2本身提供了一个回调接口,允许以最小的分配来消耗文档。为了获得更符合人体工程学的体验,可阅读下面的GettigaDOM。
costhtmlparser2=require("htmlparser2");costparser=ewhtmlparser2.Parser({oopetag(ame,attributes){/**Thisfireswheaewtagisopeed.**Ifyoudo'teedaaggregated`attributes`object,*havealookatthe`oopetagame`ad`oattribute`evets.*/if(ame==="script"&&attributes.type==="text/javascript"){cosole.log("JS!Hooray!");}},otext(text){/**Fireswheeverasectiooftextwasprocessed.**Notethatthiscafireataypoitwithitextadyoumight*havetostichtogethermultiplepieces.*/cosole.log("-->",text);},oclosetag(tagame){/**Fireswheatagisclosed.**Youcarelyothisevetolyfirigwheyouhavereceiveda*equivaletopeigtagbefore.Closigtagswithoutcorrespodig*opeigtagswillbeigored.*/if(tagame==="script"){cosole.log("That'sit?!");}},});parser.write("Xyz<scripttype='text/javascript'>costfoo='<<bar>>';</script>");parser.ed();Output(withmultipletextevetscombied):
-->XyzJS!Hooray!-->costfoo='<<bar>>';That'sit?!
评论