简体   繁体   中英

How to match and find in a raw file with regex between two strings and get the whole strings between?

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>

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

use this

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM