I want to make a function that gets a string with html code (as a string) including the script tag – for example: “<div>dkjdg</div><script>blabla</script>fgfgh<span>hey</span>” and returns all the script inside the script tag including the open and close tags.
I tried this so far:
var s;
function(string)
{
var a = string.toLowerCase().match("/(.*?)<script>(.*?)<//script>(.*?)/");
s = a[2];
return a[1]+a[3];
}
s is containing what is between the script tags and the return is every thing else.
But it is not working…
And what happens if the block looks like this?
Never parse HTML with regular expressions. Instead, you can do something like this (with a little help from jQuery):
That will do exactly what you want, doesn’t break unless the input is totally screwed and is more or less 100% cross-browser compatible. Note that we don’t want to use jQuery’s .html() to set the div content, because that will eval() the blocks!
This:
will return