const regex = /(?:(?<=^\n)|(?<=\A))
(?<list_all>
(?<list_prefix>
[\s]{0,3}
(?<list_type_any>
(?:
(?<list_type_unordered_star>(?&list_unordered_star))
|
(?<list_type_unordered_minus>(?&list_unordered_minus))
|
(?<list_type_unordered_plus>(?&list_unordered_plus))
|
(?<list_type_ordered>(?&list_ordered))
)
)
[ \t]+
)
(?<list_content>(?s:.+?))
(?<list_after>
\z
|
\n{2,}
(?=\S)
(?!
[ \t]*
(?<list_type_any2>
(?:
(?<list_type_unordered_star2>(?&list_unordered_star))
|
(?<list_type_unordered_minus2>(?&list_unordered_minus))
|
(?<list_type_unordered_plus2>(?&list_unordered_plus))
|
(?<list_type_ordered2>(?&list_ordered))
)
)
[ \t]+
)
)
)
(?(DEFINE)
(?<list_unordered_star>
[\*]
(?!
(?:
[ ]?[\*][ ]?
){2,}
)
)
(?<list_unordered_minus>
[\-]
(?!
(?:
[ ]?[\-][ ]?
){2,}
)
)
(?<list_unordered_plus>[\+])
(?<list_ordered>\d+[\.])
)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?:(?<=^\\n)|(?<=\\A))
(?<list_all>
(?<list_prefix>
[\\s]{0,3}
(?<list_type_any>
(?:
(?<list_type_unordered_star>(?&list_unordered_star))
|
(?<list_type_unordered_minus>(?&list_unordered_minus))
|
(?<list_type_unordered_plus>(?&list_unordered_plus))
|
(?<list_type_ordered>(?&list_ordered))
)
)
[ \\t]+
)
(?<list_content>(?s:.+?))
(?<list_after>
\\z
|
\\n{2,}
(?=\\S)
(?!
[ \\t]*
(?<list_type_any2>
(?:
(?<list_type_unordered_star2>(?&list_unordered_star))
|
(?<list_type_unordered_minus2>(?&list_unordered_minus))
|
(?<list_type_unordered_plus2>(?&list_unordered_plus))
|
(?<list_type_ordered2>(?&list_ordered))
)
)
[ \\t]+
)
)
)
(?(DEFINE)
(?<list_unordered_star>
[\\*]
(?!
(?:
[ ]?[\\*][ ]?
){2,}
)
)
(?<list_unordered_minus>
[\\-]
(?!
(?:
[ ]?[\\-][ ]?
){2,}
)
)
(?<list_unordered_plus>[\\+])
(?<list_ordered>\\d+[\\.])
)', 'gm')
const str = `
* asterisk 1
* asterisk 2
* asterisk 3
* * *
`;
// 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