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, '"') + '"'; } 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"yadda">blabla</script>'
Comments
Post a Comment