Search code examples
node.jsregexmarkdowncell

Regex: table line matcher


I want to parse a table line using regex.

Input

   |---|---|---|
|---|---|---|

So far I've come up with this regex:

/^(?<indent>\s*)\|(?<cell>-+|)/g

Regex101 Link: https://regex101.com/r/wzMYxd/1

But this regex is incomplete.

This only finds the first cell --|, but I want to find all the following cells as different ----|.

Question: Can we catch the following cells with the same pattern using the regex? ExpectedOutput: groups with array of matched cells: ["---|", "----|", "---|"]

Note: no constant number of - is required


Solution

  • How about first verifying, if the line matches the pattern:

    ^[ \t]*\|(?:-+\|)+$
    

    See this demo at regex101 - If it matches, extract the stuff:

    ^(?<indent>[\t ]*)\||(?<cell>-+)\|
    

    Another demo at regex101 (explanation on the right side)


    With just one regex maybe by use of sticky flag y and a lookahead for validation:

    /^(?<indent>[ \t]*)\|(?=(?:-+\|)+$)|(?!^)(?<cell>-+)\|/gy
    

    One more demo at regex101

    The lookahead checks once after the first | if the rest of the string matches the pattern. If this first match fails, due to the y flag (matches are "glued" to each other) the rest of the pattern fails too.