const regex = /^(>*([=*_]) # Match a separator type and put it in group 2, group 1 will contain the whole seperator so that we can "backreference" it in the closing tag
\2{3} # It needs to be at least 4 long let's say (1 + 3)
(?:\2|[\r\n])+) # Let's now support also newlines for "multi line seperators"
([\s\S]+?) # Match the content and put it in group 3
\1/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(>*([=*_]) # Match a separator type and put it in group 2, group 1 will contain the whole seperator so that we can "backreference" it in the closing tag
\\2{3} # It needs to be at least 4 long let\'s say (1 + 3)
(?:\\2|[\\r\\n])+) # Let\'s now support also newlines for "multi line seperators"
([\\s\\S]+?) # Match the content and put it in group 3
\\1', 'gm')
const str = `This is some text that should not be matched. As you can see, it is not enclosed
by separator lines.
===========================================================
This part should be matched as it is between two separator lines. Note that the
opening and closing separators are composed of the exact same number of the same
character.
===========================================================
This block should not be matched as it is not enclosed by its own separators,
but rather the closing separator of the previous block and the opening
separator of the next block.
===========================================================
It is tricky to distinguish between an enclosed and non-enclosed blocks, because
sometimes a matching pair of separators appears to be legal, while it is really
the closing separator of the previous block and the opening separator of the
next one (e.g. the block obove this one).
===========================================================
==================================
=====
This block is enclosed by multiline separators.
==================================
=====
Some more text that should not be matched by the regex.
***************************************
A separator can be a different character, for example the asterisk.
***************************************
***************************************
*******************
Another example of a multiline separated block.
***************************************
*******************
>Even more text not to be matchedby the regex. This time, preceeded by a
>variable number of '>'.
>>__________________________________________
>>And another type of separator. The block is now also a part of a reply section
>>of the email.
>>__________________________________________`;
// 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