regex - javascript RegExp: how to match multiline and closetag -


var ex = /(<script\s?[^>]*>)([\s\s]*)(<\/script>)/; //note: here 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 got wrong script: var x=0;</script><div>..</div><script>var y=0;

what want is: var x=0; , var y=0;

use regex below:

<script>([\s\s]*?)</script> 

in javascript cannot make . dotall use [\s\s] character class matches character either whitespace or not whitespace including newline. ? non-greedy match don't nest script tags.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -