L
L
Lorem Ipsum2021-11-13 11:37:32
JavaScript
Lorem Ipsum, 2021-11-13 11:37:32

How to extract a string from HTML via JavaScript?

I want to parse the price of the swing so I can keep track of when it drops. But I do not know how to make the script work with minimal resources.

Where to spar.

I found that there is a line in the source code (line 84) So far I have only written this code:"productCost": 16990,

if (OUTPUT_HTML.indexOf('"productCost":') !== -1) {
  const PRODUCT_COST_INDEX = OUTPUT_HTML.indexOf('"productCost":');
}


But I don't even know what to do next.

It is necessary to somehow reach the comma, but somehow competently. Tell me, please, how can I do this? Is there an easier way that I don't know about yet?

Parsing HTML via HTTPS module in Node.js

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2021-11-13
@ImpAnonym

indexOf has a second parameter.

spoiler
function findInString(OUTPUT_HTML, before, after) {
  let start = OUTPUT_HTML.indexOf(before);
  if (start === -1) 
    return '';
  start += before.length;
  
  const end = OUTPUT_HTML.indexOf(',', start);
  if (end === -1) 
    return '';
  return OUTPUT_HTML.slice(start, end);
}

const productCost = +findInString(OUTPUT_HTML, '"productCost":', ',');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question