0

I'll be doing all this is node.js In my scenario I have an html string and it contains this string:

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

is there a regular expression that could extract only the value of the token? e.g.:

MTYwNjU1NzAwOHRor9RCGkXDyFBLI7HUPCwb-v46P012KayHiFSHTKDdW7CUBvjiKTHoC3lVtRBOBIGwSRA4_ojvfiG3Khnsd54.

I've also looked into html parsing npm modules, no such luck.

djsnoob
  • 84
  • 6

3 Answers3

2

Yep, the regex is reasonably straight forward.


const htmlString = '<input type="hidden" name="token" id="token" value="MTYwNjU1NzAwOHRor9RCGkXDyFBLI7HUPCwb-v46P012KayHiFSHTKDdW7CUBvjiKTHoC3lVtRBOBIGwSRA4_ojvfiG3Khnsd54." />';
const regex = new RegExp("value=\"(.*?)\"");
result = regex.exec(token)[1];

In plain English, how the regex works is it searches the html string until it finds the characters value=", then it picks up all the next characters until the next ". Looking at the regex itself to explain more clearly:

regex = "value=\"(.*?)\""

The \" searches for the double quotes character. The slash escapes the double quote, so javascript doesn't mistakenly think you're ending the string.

The parentheses around .*? are a matching group. The reason we call the 1st element of result, i.e result = regex.exec(token)[1], is to pull out the match group

The . matches all characters, the * matches any number of them, and the ? makes it non-greedy, so it stops at the next quotation mark.

Alan
  • 1,746
  • 7
  • 21
1

You can put that value by this method:

let inputValue = getElementById(token).value;
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Hello, thanks for this comment, but ive made a mistake, im not reading a local html file, im reading response.body from a get request to a website – djsnoob Nov 28 '20 at 10:19
  • @djsnoob if I understood you, you are doint get requst to the server and get that html tag with token in value attribute? But why don`t you get just object with this token inside? Or I am not right? – Danil Safonov Nov 28 '20 at 10:23
  • because im pretty sure the get request returns a string, hence why I asked for regex @Danil Sadonov – djsnoob Nov 28 '20 at 10:27
  • @djsnoob but if you get that by response from server you will get it in string by the way, you just get or " – Danil Safonov Nov 28 '20 at 10:34
1

I've also looked into html parsing npm modules, no such luck.

You can use for example 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);

Output

MTYwNjU1NzAwOHRor9RCGkXDyFBLI7HUPCwb-v46P012KayHiFSHTKDdW7CUBvjiKTHoC3lVtRBOBIGwSRA4_ojvfiG3Khnsd54.
The fourth bird
  • 154,723
  • 16
  • 55
  • 70