$re = '@(?:<!--|-->)([\s\S]*?)-->@m';
$str = '<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try it</button>
<textarea id="demo"></textarea>
<!-- das
i
s
t
ein terst
e-->
<script>
// hallo
function myFunction() {
var str = document.documentElement.outerHTML;
alert(str);
var a = \'<!-\'+\'-\';
var b = \'--\'+\'>\';
var n = str.indexOf(a)+4;
var end = str.lastIndexOf(b)-1;
var xout= str.slice(n,end);
document.getElementById("demo").innerHTML = xout;
}
</script>
<template id="t1">
fdff
<!-- das
i
s
t
ein test
g
f-->
fgfd
</template>
</body>
</html>';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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 PHP, please visit: http://php.net/manual/en/ref.pcre.php