Why is the /pattern/ matching, but the RegExp not?
<div id="foo">
##content##
<h1>works!</h1>
##/content##
</div>
<script>
var str = document.getElementById("foo").innerHTML;
console.log(str);
var r = new RegExp("##content##([\S\s]*)##\/content##", "img");
console.log(r.exec(str)); //null
console.log(str.match(/##content##([\S\s]*)##\/content##/img)); //matches
</script>
Problem is this line:
var r = new RegExp("##content##([\S\s]*?)##\/content##", "img");
It should be replaced with:
var r = new RegExp("##content##([\\S\\s]*?)##\/content##", "img");
Reason: Understand that RegExp object takes a String as argument to construct and you need to double escape \S and \s to be correctly interpreted by RegEx engine so \S should become \\S and \s should become \\s in your regex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With