Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse HTML string to JS in Nodejs

I was wondering if there was a better way to capture a value inside an input tag rather than using regex in JS.

"<html><head></head><body onload=\"document.form1.submit()\"><form name=\"form1\" method=\"post\" action=\"\" ><input name=\"Token\" type=\"hidden\" value=\"\"><input name=\"ID\" type=\"hidden\" value=\"12120012732dafd4\"></form></body></html>"

Ideally I would like to capture just the ID value 12120012732dafd4

like image 642
Matthew Waymouth Avatar asked Oct 23 '25 02:10

Matthew Waymouth


1 Answers

Since there is no DOM in node you have to initialize a cheerio instance from an HTML string. (this example comes from the cheerio readme)

 var cheerio = require('cheerio'),
        $ = cheerio.load("<html><head></head><body onload=\"document.form1.submit()\"><form name=\"form1\" method=\"post\" action=\"\" ><input name=\"Token\" type=\"hidden\" value=\"\"><input name=\"ID\" type=\"hidden\" value=\"12120012732dafd4\"></form></body></html>"
);
    
    $('input').val();
like image 154
nullException Avatar answered Oct 25 '25 16:10

nullException



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!