javascript - Turn several strings of structured text into HTML table -
for example have following text no tags @ within html page:
color: red shape: square side: 1mm
as rows needed, 3 quite enough question. 1 be.
in rows i'll have the beginning of text string
, colon+space (: )
, the end of text string
.
how should turn the beginning of text string
<tr><td>
, colon+space
:</td><td>
, the end of text string
</td></tr>
?
thanks @andrew willems (the script) , @phil (further suggestions) , running.
the original text here has unnecessary lines before , after text demonstrate need dealing with, , ability deal with, lines.
var opening = '<table id="newborn_table"><tbody>'; var closing = '</tbody></table>'; var origtext = document.queryselector('#source').innertext; var lines = origtext.split('\n').filter(function(line) { return (line !== ""); }); var rowstext = ''; lines.foreach(function(line) { var parts = line.split(': '); rowstext += '<tr><td>' + parts[0] + '</td><td>' + parts[1] + '</td></tr>' }); document.queryselector('#result').innerhtml = opening + rowstext + closing;
#newborn_table td { border: solid red 1px; }
<p>original text:<p> <pre id="source"> color: red shape: square side: 1mm </pre> <p>parsed table:</p> <div id="result"></div>
Comments
Post a Comment