I have this data, effectively a markdown table:
blah blah blah
| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
blah blah
blah
I would like to do a regex match and get the values between the pipes out.
Ideally, I'd like to get a group for each line, and then a subgroup with each 'cell'.
Even more ideally, I'd like to make sure there are the same number of cells/columns in each row.
My attempts either match the last cell in the row/table, or every second cell. This just bombs out totally: \\|(?:([^\\r\\n\\|]*)\\|)+\\r?\\n\\|(?:(\\:?-+\\:?)\\|)+\\r?\\n(\\|(?:([^\\r\\n\\|]*)\\|)+\\r?\\n)+
This is also pretty dismal: ^#(?:([^#]+)#)+$
Looking for a solution in javascript or C#.
I'd go in js with something like this, but only if you don't need the regex for something else ;-)
var yourStr
var groups = yourStr.split('\n')
var cells = yourStr.split('|')
if (cells.length < asItShouldBe) { ... }
var cell1 = cells[1]
var cell2 = cells[2]
...
Try
var input = document.querySelectorAll("pre")[0].innerText; var output = input.match(/\\s+|\\w+-\\w+|\\w+|\\W+\\d+|\\d+/gi) .map(function(word, i) { return word.replace(/\\||\\s|--+|\\s-\\s+|:/gi, "") }); document.body.insertBefore( document.createTextNode(output.join(" ")) , document.getElementsByTagName("hr")[0] );
<hr /> <pre> blah blah blah | Tables | Are | Cool | | ------------- |:-------------:| -----:| | col 3 is | right-aligned | $1600 | | col 2 is | centered | $12 | | zebra stripes | are neat | $1 | blah blah blah </pre>
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.