2

I'm going to extract some data from a site, I want to find the string between two specific string and return in, how can I handle it? I am using JavaScript.

I'm going to make it clear with adding the string I want to do extraction on, for example this is the text that I got :

<td class="nf"><span class="low">(0.08%) 40</span></td>
<td>48,180</td>
<td>48,460</td>
<td>asasasas</td>
<td class="chart-td"><a data-tooltip="saasasa" class="chart-icon" target="_blank" href="chart/price_aed"></a></td>
</tr>
<tr data-market-row="price_try"
  data-title="<div class='tooltip-row'><span class='tooltip-row-txt'>25920 aasas sasaa </span><span class='tooltip-row-change'><span class='type low'>(0.12%) 30</span></span></div><hr><div class='tooltip-row'><span class='tooltip-row-txt'>25940 asasa </span><span class='tooltip-row-change'><span class='type low'>(0.04%) 10</span></span></div><hr><div class='tooltip-row'><span class='tooltip-row-txt'>25930 sasaas </span><span class='tooltip-row-change'><span class='type low'>(0.08%) 20</span></span></div><hr> <div class='highlight'>assasa25,990</div><div class='tooltip-info'>sasasasa</div>"
  class="pointer " onclick="if (!window.__cfRLUnblockHandlers) return false; window.location='chart/price_try'"
  data-price="25,920" data-cf-modified-4bf6cd1a2128b0ccaf953372-="">
  <th><span class="mini-flag flag-tr"></span>skkss </th>
  <td class="nf">25,920</td>
  <td class="nf"><span class="low">(0.12%) 30</span></td>
  <td>25,860</td>
  <td>26,010</td>
  <td>sssss</td>
  <td class="chart-td"><a data-tooltip="sssss" class="chart-icon" target="_blank" href="chart/price_try"></a></td>
</tr>
<tr data-market-row="price_cny"
  data-title="<div class='tooltip-row'><span class='tooltip-row-txt'>24830 saasa asasa </span><span class='tooltip-row-change'><span class='type low'>(0.08%) 20</span></span></div><hr><div class='tooltip-row'><span class='tooltip-row-txt'>24820 222 ssa </span><span class='tooltip-row-change'><span class='type low'>(0.12%) 30</span></span></div><hr><div class='tooltip-row'><span class='tooltip-row-txt'>24830 2ss 22222 </span><span class='tooltip-row-change'><span class='type low'>(0.08%) 20</span></span></div><hr> <div class='highlight'>sasasas: 24,890</div><div class='tooltip-info'>ssssaaa</div>"
  class="pointer " onclick="if (!window.__cfRLUnblockHandlers) return false; window.location='chart/price_cny'"
  data-price="24,830" data-cf-modified-4bf6cd1a2128b0ccaf953372-="">
  <th><span class="mini-flag flag-cn"></span>ssssss </th>
  <td class="nf">24,830</td>
  <td class="nf"><span class="low">(0.08%) 20</span></td>

I want to retrieve all strings which string with <tr data-market-row="price_try" and ends with /price_try"></a></td></tr>

Syntle
  • 4,700
  • 3
  • 9
  • 32
Amir Doreh
  • 737
  • 1
  • 7
  • 19
  • Your final sentence before "thank you" is not grammatically correct and seems to be missing some key words ("begins", perhaps?), so it is not clear what you are asking: *which string with* makes no sense. – Booboo May 31 '20 at 20:26
  • "string with" -> "starts with" probably – Jonas Berlin May 31 '20 at 22:00
  • 1
    You should clarify your needs by giving a concrete example of expected result, based on the HTML submitted. Parsing HTML with regex is not a good idea, and since your question is tagged Javascript use the [DOMParser](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser) instead. – Anonymous May 31 '20 at 22:28
  • guys, thank you all for your comments. i was late and i didnt pay attention to the grammar @Booboo – Amir Doreh Jun 01 '20 at 10:58
  • @Anonymous how can i use it for extracting that part that i mentioned? – Amir Doreh Jun 01 '20 at 10:58
  • i want the number inside this tag `25,920` between `` – Amir Doreh Jun 01 '20 at 11:02
  • Is this the complete HTMl or there is more ? If you have a complete table starting and ending with `` tags it is possible to use the DOM parser or use the table as a selector. You have some examples [here](https://stackoverflow.com/questions/3065342/how-do-i-iterate-through-table-rows-and-cells-in-javascript). Probably you will get better answers if you can add a more complete example of table. You can keep just a few rows if there are too many.
    – Anonymous Jun 01 '20 at 15:30
  • thank you so much post it as answer im gonna vote it up – Amir Doreh Jun 01 '20 at 16:30
  • @Anonymous .... – Amir Doreh Jun 01 '20 at 16:30

2 Answers2

2
str.replace(/\r?\n|\r/g, '').match(/<tr data-market-row="price_try"(.*)\/price_try"><\/a><\/td><\/tr>/)[1];

use this

em.d
  • 160
  • 9
1

assuming the string

let str = `<td class="nf"><span class="low">(0.08%) 40</span></td>
<td>48,180</td>
<td>48,460</td>
<td>asasasas</td>
<td class="chart-td"><a data-tooltip="saasasa" class="chart-icon" target="_blank" href="chart/price_aed"></a></td>
</tr>
<tr data-market-row="price_try"
  data-title="<div class='tooltip-row'><span class='tooltip-row-txt'>25920 aasas sasaa </span><span class='tooltip-row-change'><span class='type low'>(0.12%) 30</span></span></div><hr><div class='tooltip-row'><span class='tooltip-row-txt'>25940 asasa </span><span class='tooltip-row-change'><span class='type low'>(0.04%) 10</span></span></div><hr><div class='tooltip-row'><span class='tooltip-row-txt'>25930 sasaas </span><span class='tooltip-row-change'><span class='type low'>(0.08%) 20</span></span></div><hr> <div class='highlight'>assasa25,990</div><div class='tooltip-info'>sasasasa</div>"
  class="pointer " onclick="if (!window.__cfRLUnblockHandlers) return false; window.location='chart/price_try'"
  data-price="25,920" data-cf-modified-4bf6cd1a2128b0ccaf953372-="">
  <th><span class="mini-flag flag-tr"></span>skkss </th>
  <td class="nf">25,920</td>
  <td class="nf"><span class="low">(0.12%) 30</span></td>
  <td>25,860</td>
  <td>26,010</td>
  <td>sssss</td>
  <td class="chart-td"><a data-tooltip="sssss" class="chart-icon" target="_blank" href="chart/price_try"></a></td>
</tr>
<tr data-market-row="price_cny"
  data-title="<div class='tooltip-row'><span class='tooltip-row-txt'>24830 saasa asasa </span><span class='tooltip-row-change'><span class='type low'>(0.08%) 20</span></span></div><hr><div class='tooltip-row'><span class='tooltip-row-txt'>24820 222 ssa </span><span class='tooltip-row-change'><span class='type low'>(0.12%) 30</span></span></div><hr><div class='tooltip-row'><span class='tooltip-row-txt'>24830 2ss 22222 </span><span class='tooltip-row-change'><span class='type low'>(0.08%) 20</span></span></div><hr> <div class='highlight'>sasasas: 24,890</div><div class='tooltip-info'>ssssaaa</div>"
  class="pointer " onclick="if (!window.__cfRLUnblockHandlers) return false; window.location='chart/price_cny'"
  data-price="24,830" data-cf-modified-4bf6cd1a2128b0ccaf953372-="">
  <th><span class="mini-flag flag-cn"></span>ssssss </th>
  <td class="nf">24,830</td>
  <td class="nf"><span class="low">(0.08%) 20</span></td>`

you can extract your thing with the following regex:

str.replace(/\r?\n|\r/g, '').match(/<tr data-market-row="price_try"(.*)\/price_try"><\/a><\/td><\/tr>/)[1];
Kamil Janowski
  • 1,294
  • 1
  • 15
  • 31