Warm tip: This article is reproduced from serverfault.com, please click

javascript-是否存在使用正则表达式从HTML字符串提取数据的有效方法?

(javascript - Is there an efficient way to use regular expressions to extract data from an HTML string?)

发布于 2020-11-28 10:05:01

我要做的全部是node.js在我的场景中,我有一个html字符串,它包含以下字符串:

// there is html code above ^^^
<input type="hidden" name="token" id="token" value="MTYwNjU1NzAwOHRor9RCGkXDyFBLI7HUPCwb-v46P012KayHiFSHTKDdW7CUBvjiKTHoC3lVtRBOBIGwSRA4_ojvfiG3Khnsd54." />
//and html code below vvv

是否有一个仅可提取令牌值的正则表达式?例如:

MTYwNjU1NzAwOHRor9RCGkXDyFBLI7HUPCwb-v46P012KayHiFSHTKDdW7CUBvjiKTHoC3lVtRBOBIGwSRA4_ojvfiG3Khnsd54.

我也研究了html解析npm模块,没有这种运气。

Questioner
djsnoob
Viewed
22
The fourth bird 2020-11-28 21:27:57

我也研究了html解析npm模块,没有这种运气。

你可以使用例如jsdom

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const dom = new JSDOM(`<input type="hidden" name="token" id="token" value="MTYwNjU1NzAwOHRor9RCGkXDyFBLI7HUPCwb-v46P012KayHiFSHTKDdW7CUBvjiKTHoC3lVtRBOBIGwSRA4_ojvfiG3Khnsd54." />`);
let elm = dom.window.document.getElementById("token");
if (elm) console.log(elm.value);

输出

MTYwNjU1NzAwOHRor9RCGkXDyFBLI7HUPCwb-v46P012KayHiFSHTKDdW7CUBvjiKTHoC3lVtRBOBIGwSRA4_ojvfiG3Khnsd54.