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

Share Improve this question asked Dec 15, 2020 at 7:15 Matthew WaymouthMatthew Waymouth 1212 silver badges4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Since 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

Share Improve this question asked Dec 15, 2020 at 7:15 Matthew WaymouthMatthew Waymouth 1212 silver badges4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Since 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