const regex = /(?|
\G(?!\A) # contiguous to the precedent match (inside a comment)
(?|
-{2,}+([^->][^-]*) # duplicate hyphens, not part of the closing sequence
|
(-[^-]+) # preserve isolated hyphens
|
-+ (?=-->) # hyphens before closing sequence, break the contiguity
|
-->[^<]* (*SKIP)(*FAIL) # closing sequence, go to next <, break contiguity
)
|
[^<]*<+ # reach the next < (outside comment)
(?> [^<]+ <+ )*? # all characters until <!-- (include)
(?: !-- \K | [^<]*\z\K (*ACCEPT) ) # new comment or end of the string
(?|
-*+ ([^->][^-]*) # possible hyphens not followed by >
|
-+ (?=-->) # hyphens before closing sequence, break the contiguity
|
-?+ ([^-]+) # one hyphen followed by >
|
-->[^<]* (*SKIP)(*FAIL) () # closing sequence, go to next <, break contiguity
) # () avoids a misterious bug in regex101, you can remove it
)
/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?|
\\G(?!\\A) # contiguous to the precedent match (inside a comment)
(?|
-{2,}+([^->][^-]*) # duplicate hyphens, not part of the closing sequence
|
(-[^-]+) # preserve isolated hyphens
|
-+ (?=-->) # hyphens before closing sequence, break the contiguity
|
-->[^<]* (*SKIP)(*FAIL) # closing sequence, go to next <, break contiguity
)
|
[^<]*<+ # reach the next < (outside comment)
(?> [^<]+ <+ )*? # all characters until <!-- (include)
(?: !-- \\K | [^<]*\\z\\K (*ACCEPT) ) # new comment or end of the string
(?|
-*+ ([^->][^-]*) # possible hyphens not followed by >
|
-+ (?=-->) # hyphens before closing sequence, break the contiguity
|
-?+ ([^-]+) # one hyphen followed by >
|
-->[^<]* (*SKIP)(*FAIL) () # closing sequence, go to next <, break contiguity
) # () avoids a misterious bug in regex101, you can remove it
)
', 'g')
const str = `<!----------------- toto -->
<!-- toto ---------------------------->
<!---->
<!----->
<!--->>>>--->
asdsd sadasdsad sadasd asdasd asdasd --> asd asdasd
<!--- ->>>>>>
---------------------------------------
***************************************
AUGMENTATION BLUEPRINTS
***************************************
---------------------------------------
--->----
<!-- toto-toto -->
<!--OTHER IDEAS --
<anytaghere>
<!----PROBABLY NOT:---
<whatever>
--><!--`;
const subst = `\1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
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