regex - Javascript regular expression all between <script> tag -


i want make function gets string html code (as string) including script tag - example: "<div>dkjdg</div><script>blabla</script>fgfgh<span>hey</span>" , returns script inside script tag including open , close tags.

i tried far:

var s; function(string) {     var = string.tolowercase().match("/(.*?)<script>(.*?)<//script>(.*?)/");     s = a[2];     return a[1]+a[3]; } 

s containing between script tags , return every thing else.

but not working...

and happens if block looks this?

<script type="text/javascript"><![cdata[   alert('hello </script>'); ]]></script> 

never parse html regular expressions. instead, can (with little jquery):

function getscript(str) {   var div = $('<div>'), tmpdiv = $('<div>'), scr, ret = '<script';   div[0].innerhtml = str;   scr = div.find('script');    ( var = 0; < scr[0].attributes.length; i++ ) {     var attr = scr[0].attributes[i];      if ( attr.value && attr.value != '' )       ret += ' ' + attr.name + '="' +               tmpdiv.text(attr.value).html().replace(/"/g, '&quot;') +              '"';   }    return ret + '>' + scr.text() + '</script>'; } 

that want, doesn't break unless input totally screwed , more or less 100% cross-browser compatible. note don't want use jquery's .html() set div content, because eval() blocks!

this:

getscript("<div>dkjdg</div><script type='yadda\"yadda'>blabla</script>fgfgh<span>hey</span>") 

will return

'<script type="yadda&quot;yadda">blabla</script>' 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -