var ex = /(<script\s?[^>]*>)([\s\S]*)(<\/script>)/;
//Note: here is 2 script tags
var str = '<script>\nvar x=0;\n</script>\n<div>\nhtml\n</div>\n<script>var y=0;\n</script>'
str.replace(ex, function(full, prefix, script, suffix) {
return prefix + doSomething(script) + suffix;
})
But I got wrong script: var x=0;</script><div>..</div><script>var y=0;
What I want is: var x=0;
and var y=0;
Use regex like below:
<script>([\s\S]*?)</script>
In Javascript we cannot make .
dotall so we use the [\s\S] character class which matches any character either whitespace or not whitespace including newline. ?
is for non-greedy match so that you don't nest script tags.
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