Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parsing table-like string into JavaScript object

This string is structured in a human-readable table-like way. It contains three columns. However, the only information I need is a list of all of the values from the first column.

app115                                115.115                              winget
app225                                115.115Chrome                        winget
Knotes                                1MHz.Knotes                          winget
BPMN-RPA Studio                       1ic.BPMN-RPAstudio                   winget
Fishing Funds                         1zilc.FishingFunds                   winget
3601                                  360.360Chrome                        winget
3602                                  360.360Chrome.X                      winget
3603                                  360.360CleanMaster                   winget
3604                                  360.360se                            winget
3CX Call Flow Designer (.exe edition) 3CX.3CXCallFlowDesigner              winget

Using javascript, how would I parse this string to get a result of something like:

['app115', 'app225', 'Knotes', 'BPMN-RPA Studio', 'Fishing Funds', '360', '360', '360', '360', '3CX Call Flow Designer (.exe edition)']

Here are some of my ideas that I couldn’t get to work:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

step 1, since the second two columns are not necessary, we can start by replacing ‘winget’ with blank text string1.replaceAll("winget", "") this removes the whole left column because all values in that column are ‘winget’.

step 2, remove all occurrences of multiple characters that are surrounded by 2 or more spaces on each side. This should get rid of the whole second column because each value has at least two spaces on each side.will not work because if the value in the first column is too long, the value in the second column may only have one space next to it. Check the last row of the original string.

last step, once string now looks something like: "app115 app225 Knotes BPMN-RPA Studio Fishing Funds...", make into an array using string.split(" ")

Hope my question makes sense.
Thanks for any help

>Solution :

This one works for me

Assuming any kind of whitespace including tabs between the columns

\s+\w+\. is the leading whitespace BEFORE a string with a fullstop

const lines = table.split(/\r?\n/)
const column1 = lines.map(line => line.split(/\s+\w+\./)[0])
console.log(column1)
<script>
const table = `app115                                115.115                              winget
app225                                115.115Chrome                        winget
Knotes                                1MHz.Knotes                          winget
BPMN-RPA Studio                       1ic.BPMN-RPAstudio                   winget
Fishing Funds                         1zilc.FishingFunds                   winget
3601                                  360.360Chrome                        winget
3602                                  360.360Chrome.X                      winget
3603                                  360.360CleanMaster                   winget
3604                                  360.360se                            winget
3CX Call Flow Designer (.exe edition) 3CX.3CXCallFlowDesigner              winget`
</script>

If column1 is always 38 chars long, then

const lines = table.split(/\r?\n/)
const column1 = lines.map(line => line.slice(0,38).trim())
console.log(column1)
<script>
const table = `app115                                115.115                              winget
app225                                115.115Chrome                        winget
Knotes                                1MHz.Knotes                          winget
BPMN-RPA Studio                       1ic.BPMN-RPAstudio                   winget
Fishing Funds                         1zilc.FishingFunds                   winget
3601                                  360.360Chrome                        winget
3602                                  360.360Chrome.X                      winget
3603                                  360.360CleanMaster                   winget
3604                                  360.360se                            winget
3CX Call Flow Designer (.exe edition) 3CX.3CXCallFlowDesigner              winget`
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading