I am trying the regex ^(?<=[\s]*namespace[\s]*---+\s+)(.|\s)+(?=\(\s*\d+\s*rows\))/gm to extract row items from single column tabular list format string.
But the leading spaces are added in the match.
The \s+ operators in the lookahead and lookbehind groups do not help. Refer below:
x = `namespace
-------------------
itm1
itm2
itm3
itm4
(4 rows)
`
console.log(x.match(/^(?<=[\s]*namespace[\s]*---+\s+)(.|\s)+(?=\(\s*\d+\s*rows\))/gm)[0].split(/\s+/))
Output is with leading and trailing spaces as separate list elements:
[ '', 'itm1', 'itm2', 'itm3', 'itm4', '' ]
But with console.log(x.match(/^(?<=[\s]*namespace[\s]*---+\s+)(.|\s)+(?=\(\s*\d+\s*rows\))/gm)[0].trim().split(/\s+/)) <-- notice the trim() before the split(..), the output is:
[ 'itm1', 'itm2', 'itm3', 'itm4' ]
Why does the \s+ at the end of the lookahead group (?<=[\s]*namespace[\s]*---+\s+) not remove all the spaces before the desired matching group caught by (.|\s)+.