const regex = /((?<=\|\s)([^|]+)(?!\|\s))/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('((?<=\\|\\s)([^|]+)(?!\\|\\s))', 'gm')
const str = `- [x] Example of a completed task.
- [x] ! Example of a completed task.
- [x] ? Example of a completed task.
| | Task name | Plan | Actual | File |
| :---- | :-------------------------------------- | :---------: | :---------: | :------------: |
| [x] | Task one with a reasonably long name. | 08:00-08:45 | 08:00-09:00 | [[task-one]] |
| [x] ! | Task two with a reasonably long name. | 09:00-09:30 | | [[task-two]] |
| [x] ? | Task three with a reasonably long name. | 11:00-13:00 | | [[task-three]] |
Description:
\$1: the left [ and the right ] brackets when the symbol x is in-between
\$2: the symbol x in between the brackets [ and ]
\$3: the modifier ! that follows after [x]
\$4: the modifier ? that follows after [x]
\$5: the text that follows [x] without a modifier, e.g., [x] This is targeted.
\$6: the text that follows [x] !
\$7: the text that follows [x] ?
Groups \$5 to \$7 should exclude the any | symbols in their captures.
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions