Reputation: 3112
I am using Node.js and i need to parse a html file. Now i have used htmlparser2 and it parses string in parser.write("String") method. Can i parse a html file using html parser? If yes then how?
Help is appreciated?
Upvotes: 5
Views: 6422
Reputation: 11
import * as htmlparser2 from "htmlparser2";
const parser = new htmlparser2.Parser({
onopentag(name, attributes) {
if (name === "script" && attributes.type === "text/javascript") {
console.log("JS! Hooray!");
}
},
ontext(text) {
console.log("-->", text);
},
onclosetag(tagname) {
if (tagname === "script") {
console.log("That's it?!");
}
},
});
parser. write(
"Xyz <script type='text/javascript'>const foo = '<<bar>>';</script>",
);
parser.end();
Upvotes: 1
Reputation: 497
var htmlparser = require("htmlparser2");
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
console.log("JS! Hooray!");
}
},
ontext: function(text){
console.log("-->", text);
},
onclosetag: function(tagname){
if(tagname === "script"){
console.log("That's it?!");
}
}
}, {decodeEntities: true});
parser.write("Xyz <script type='text/javascript'>var foo = '<<bar>>';</script>");
parser.end();
https://github.com/fb55/htmlparser2
http://demos.forbeslindesay.co.uk/htmlparser2/
Upvotes: -6