I have the following HTML & JavaScript code: I'm trying to extract every debtor name and his address.
function extractBetween(string){
var start_pos = string.indexOf('\tDebtor Names:\t') 1;
var end_pos = string.indexOf('\tSecured Party Names:\t',start_pos);
var extract = string.substring(start_pos,end_pos);
return extract;
}
var array = Array.from(document.querySelectorAll('table[width="98%"] table:nth-child(1) tbody')).map(
x => JSON.stringify(x.innerText.trim())
);
for(var i = 0; i < array.length; i ){
console.log(array[i]);
console.log(extractBetween(array[i]));
}
<table border="1" width="98%" align="CENTER"><tbody><tr><td>
<table align="CENTER" width="100%">
<tbody><tr>
<td bgcolor="FFF4C1" align="left"><font size="-1"> 4501.</font></td>
<td bgcolor="FFF4D4" scope="row" width="60" align="left"><font size="-2"><b>Debtor Names:</b></font></td>
<td bgcolor="FFF4D4" align="left"><font size="-1"><b> LOFLI</b></font></td>
<td bgcolor="FFF4D4" align="center"><font size="-1"><b>650USA</b></font></td>
</tr>
<tr>
<td bgcolor="LIGHTGREY"></td>
<td bgcolor="LIGHTGREY" scope="row" width="60" align="left"><font size="-2"><b>Secured Party Names:</b></font></td>
<td bgcolor="LIGHTGREY" align="left"><font size="-1"><b>METROP</b></font></td>
<td bgcolor="LIGHTGREY" align="center"><font size="-1"><b>99 USA</b></font></td>
</tr>
<tr>
<td bgcolor="FFF4D4"></td>
<td bgcolor="FFF4D4" width="60" align="left"></td>
<td bgcolor="FFF4D4" align="left"><font size="-1"><b>CTC</b></font></td>
<td bgcolor="FFF4D4" align="center"><font size="-1"><b>330USA</b></font></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody></table>
<table align="CENTER" width="100%">
<tbody><tr>
<th scope="col" bgcolor="#339999"><font size="-1">File no.</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">File Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Lapse Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Filing Type</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Pages</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Image</font></th>
</tr>
<tr>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 201807135863332</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2018</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> FS</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 1</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> </font></td>
</tr>
<tr>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 201808065965587</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 08/06/2018</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> Assignment</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 2</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> </font></td>
</tr>
<tr>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 202007246956275</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/24/2020</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> Termination</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 1</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> </font></td>
</tr>
</tbody></table>
</td></tr></tbody></table>,<table align="CENTER" width="100%">
<tbody><tr>
<th scope="col" bgcolor="#339999"><font size="-1">File no.</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">File Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Lapse Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Filing Type</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Pages</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Image</font></th>
</tr>
<tr>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 201807135863332</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2018</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> FS</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 1</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"></font></td>
</tr>
<tr>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 201808065965587</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 08/06/2018</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> Assignment</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 2</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> </font></td>
</tr>
<tr>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 202007246956275</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/24/2020</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> Termination</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 1</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> </font></td>
</tr>
</tbody></table>,<table border="1" width="98%" align="CENTER"><tbody><tr><td>
<table align="CENTER" width="100%">
<tbody><tr>
<td bgcolor="FFF4C1" align="left"><font size="-1"> 4502.</font></td>
<td bgcolor="FFF4D4" scope="row" width="60" align="left"><font size="-2"><b>Debtor Names:</b></font></td>
<td bgcolor="FFF4D4" align="left"><font size="-1"><b> CHAI</b></font></td>
<td bgcolor="FFF4D4" align="center"><font size="-1"><b>54 SUNRISE USA</b></font></td>
</tr>
<tr>
<td bgcolor="LIGHTGREY"></td>
<td bgcolor="LIGHTGREY" width="60" align="left"></td>
<td bgcolor="LIGHTGREY" align="left"><font size="-1"><b>SIK</b></font></td>
<td bgcolor="LIGHTGREY" align="center"><font size="-1"><b>54 10304, USA</b></font></td>
</tr>
<tr>
<td bgcolor="FFF4D4"></td>
<td bgcolor="FFF4D4" scope="row" width="60" align="left"><font size="-2"><b>Secured Party Names:</b></font></td>
<td bgcolor="FFF4D4" align="left"><font size="-1"><b>METRO</b></font></td>
<td bgcolor="FFF4D4" align="center"><font size="-1"><b>PARK AVE.USA</b></font></td>
</tr>
<tr>
<td bgcolor="LIGHTGREY"></td>
<td bgcolor="LIGHTGREY" width="60" align="left"></td>
<td bgcolor="LIGHTGREY" align="left"><font size="-1"><b>CTC</b></font></td>
<td bgcolor="LIGHTGREY" align="center"><font size="-1"><b>USA</b></font></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody></table>
<table align="CENTER" width="100%">
<tbody><tr>
<th scope="col" bgcolor="#339999"><font size="-1">File no.</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">File Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Lapse Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Filing Type</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Pages</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Image</font></th>
</tr>
<tr>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 201807135864485</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2018</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> FS</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 1</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> </font></td>
</tr>
<tr>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 201808065965614</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 08/06/2018</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> Assignment</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 2</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> </font></td>
</tr>
<tr>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 202006055863845</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 06/05/2020</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> Termination</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 1</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> </font></td>
</tr>
</tbody></table>
</td></tr></tbody></table>,<table align="CENTER" width="100%">
<tbody><tr>
<th scope="col" bgcolor="#339999"><font size="-1">File no.</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">File Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Lapse Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Filing Type</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Pages</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Image</font></th>
</tr>
<tr>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 201807135864485</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2018</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> FS</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 1</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> </font></td>
</tr>
<tr>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 201808065965614</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 08/06/2018</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> Assignment</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 2</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> </font></td>
</tr>
<tr>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 202006055863845</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 06/05/2020</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> Termination</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 1</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> </font></td>
</tr>
</tbody></table>,<table border="1" width="98%" align="CENTER"><tbody><tr><td>
<table align="CENTER" width="100%">
<tbody><tr>
<td bgcolor="FFF4C1" align="left"><font size="-1"> 4503.</font></td>
<td bgcolor="FFF4D4" scope="row" width="60" align="left"><font size="-2"><b>Debtor Names:</b></font></td>
<td bgcolor="FFF4D4" align="left"><font size="-1"><b> PAN.</b></font></td>
<td bgcolor="FFF4D4" align="center"><font size="-1"><b>USA</b></font></td>
</tr>
<tr>
<td bgcolor="LIGHTGREY"></td>
<td bgcolor="LIGHTGREY" scope="row" width="60" align="left"><font size="-2"><b>Secured Party Names:</b></font></td>
<td bgcolor="LIGHTGREY" align="left"><font size="-1"><b>CTC</b></font></td>
<td bgcolor="LIGHTGREY" align="center"><font size="-1"><b>USA</b></font></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody></table>
<table align="CENTER" width="100%">
<tbody><tr>
<th scope="col" bgcolor="#339999"><font size="-1">File no.</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">File Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Lapse Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Filing Type</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Pages</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Image</font></th>
</tr>
<tr>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 201807135866148</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2018</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> FS</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 1</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> </font></td>
</tr>
<tr>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 202105105737065</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 05/10/2021</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> Termination</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 1</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1" color="RED"> NA * </font></td>
</tr>
</tbody></table>
</td></tr></tbody></table>,<table align="CENTER" width="100%">
<tbody><tr>
<th scope="col" bgcolor="#339999"><font size="-1">File no.</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">File Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Lapse Date</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Filing Type</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Pages</font></th>
<th scope="col" bgcolor="#339999"><font size="-1">Image</font></th>
</tr>
<tr>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 201807135866148</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2018</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> FS</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> 1</font></td>
<td align="center" bgcolor="FFF4D4"><font size="-1"> </font></td>
</tr>
<tr>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 202105105737065</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 05/10/2021</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 07/13/2023</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> Termination</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1"> 1</font></td>
<td align="center" bgcolor="LIGHTGREY"><font size="-1" color="RED"> NA * </font></td>
</tr>
</tbody></table>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
I can see every table text extracted from the first console.log
but the specific text I need from the function extractBetween
is always empty even though I tested the function and it is working. Any help on how to solve this issue please? Thanks.
CodePudding user response:
Try this
function extractBetween(string){
var start_pos = string.indexOf('Debtor Names:') 13;
var end_pos = string.indexOf('Secured Party Names:');
var extract = string.substring(start_pos,end_pos);
extract = extract.replace(/\t|\\t|\n|\\n/g, "");
return extract;
}
var array = Array.from(document.querySelectorAll('table[width="98%"] table:nth-child(1) tbody')).map(
x => JSON.stringify(x.innerText.trim())
);
for(var i = 0; i < array.length; i ){
console.log(array[i]);
console.log(extractBetween(array[i]));
}
It seems some of your \t
and \n
are double escaped. With the regex I added to the replace of extract its displaying correctly.
CodePudding user response:
You can't rely on \t
for string match inside indexOf
.
Try this:
function extractBetween(string){
var start_pos = string.indexOf("Debtor Names:");
var end_pos = string.indexOf('Secured Party Names:',start_pos);
var extract = string.substring(start_pos,end_pos);
return extract.trim();
}
var array = Array.from(document.querySelectorAll('table[width="98%"] table:nth-child(1) tbody')).map(
x => JSON.stringify(x.innerText.trim())
);
for(var i = 0; i < array.length; i ){
console.log(array[i]);
console.log(extractBetween(array[i]));
}