admin管理员组文章数量:1022761
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
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
3 Answers
Reset to default 3Since there is no DOM in node you have to initialize a cheerio instance from an HTML string. (this example es 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();
You can use cheerio:
h = "[your HTML]"
const $ = cheerio.load(h)
console.log("Value:", $("form input[name='ID']").attr("value"))
Demo: https://runkit./adelriosantiago/get-attr-from-html-in-node
Alternatively you can use jsdom or htmlparser.
You can do this with JSDom like this:
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const htmlString = /*html*/`<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>`
const dom = new JSDOM(htmlString);
const value = dom.window.document.querySelector("input[name='ID']").value
console.log(value); // "12120012732dafd4"
Demo in RunKit
Further Reading:
- HTML-parser on Node.js
- How do I parse a HTML page with Node.js
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
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
3 Answers
Reset to default 3Since there is no DOM in node you have to initialize a cheerio instance from an HTML string. (this example es 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();
You can use cheerio:
h = "[your HTML]"
const $ = cheerio.load(h)
console.log("Value:", $("form input[name='ID']").attr("value"))
Demo: https://runkit./adelriosantiago/get-attr-from-html-in-node
Alternatively you can use jsdom or htmlparser.
You can do this with JSDom like this:
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const htmlString = /*html*/`<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>`
const dom = new JSDOM(htmlString);
const value = dom.window.document.querySelector("input[name='ID']").value
console.log(value); // "12120012732dafd4"
Demo in RunKit
Further Reading:
- HTML-parser on Node.js
- How do I parse a HTML page with Node.js
本文标签: javascriptParse HTML string to JS in NodejsStack Overflow
版权声明:本文标题:javascript - Parse HTML string to JS in Nodejs - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745470120a2152069.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论