JSFiddle - React, Tailwind, and code Playground
by brigand
HTML
<pre id="out"></pre>
JavaScript
const input =
`Host dnsmgr Username Refresh State Reg.Time
dummyValue Y 1234 12 something Tue, 30 Jun 2020 06:04:45
other Value Is Here Y 7899 1234 some else Tue, 30 Jun 2020 06:03:46`;
const lines = input.split('\n').filter(Boolean);
const header = lines.shift();
const slices = [];
header.replace(/(\S+)(\s*)/g, (m, name, sp) => {
const start = slices.length ? slices[slices.length - 1].end: 0;
const end = start + [...name].length + [...sp].length;
slices.push({ start, end, name });
});
if (slices.length) {
slices[slices.length - 1].end = Infinity;
}
const parsed = lines.map(line => {
const chars = [...line];
const results = {};
for (const slice of slices) {
results[slice.name] = chars.slice(slice.start, slice.end).join('').trim();
}
return results;
});
out.textContent = JSON.stringify(parsed, null, 2);