skip to Main Content

code should be something like this:

const mdtext = `some blabla text
Here is a table:

| a | b |
|---|---|
| 1 | 2 |
| ^ | 3 |

more text outside

Here goes another table

| apples | oranges | bananas |
|--------|---------|---------|
| 11 | 24 | 22 |
| 84 | 32 | 23 |

- some bullets
- more bullets


`;

const count = countTables(mdtext)
console.log(count); // it should output 2


This regex https://stackoverflow.com/a/29616512/865220 matches single header only, I need with multiple.

2

Answers


  1. Chosen as BEST ANSWER

    I have found a non-regex way to do it, It uses remark and rehype libraries.

    import rehypeSanitize from 'rehype-sanitize'
    import rehypeStringify from 'rehype-stringify'
    import remarkParse from 'remark-parse'
    import remarkRehype from 'remark-rehype'
    import { unified } from 'unified'
    
    function countTables(fileContent) {
      const file = await unified()
        .use(remarkParse)
        .use(remarkRehype)
        .use(rehypeSanitize)
        .use(rehypeStringify)
        .parse(fileContent)
    
      const elems = file.children
      let count = 0
      for (let i = 0; i < elems.length; i++) {
        if (elems[i].type == 'paragraph') {
          if (elems[i].children.length == 1) {
            if (elems[i].children[0].type == 'text') {
              const line0 = elems[i].children[0].value.trim().split('n')[0]
              if (line0[0] == '|' && line0[line0.length - 1] == '|') {
                count = count + 1;
              }
            }
          } else if (elems[i].children.length >= 3) {
            if (
              elems[i].children[0].type == 'text' &&
              elems[i].children[1].type == 'break' &&
              elems[i].children[2].type == 'text'
            ) {
              const line0 = elems[i].children[0].value.trim().split('n')[0].trim()
              if (line0[0] == '|' && line0[line0.length - 1] == '|') {
                count = count + 1;
              }
            }
          }
        }
      }
      return count
    }
    
    

  2. For your sample data, I would suggest the following regex:

    ^(?:[ t]*|(?:.+?|)+[ t]*(?:[rn]|$))+.

    Check it on regex101.

    An explanation using artificial intelligence can be found here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search