If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

but i would like to fully remove this stuff from the html. i'm using JQuery $.get() and i need to remove scripts from the returned data before any manipulations with it, because i put a portion of the returned data into a temporary div to be able to search through its elements and i do not need scripts in there.

i tried to use this one

Code:

/<\s*script[^>]*>[\w\W\s]*<\s*\/script>/ig;

sometimes it matches but sometimes it does not. can anybody help? thanks in advance

xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"yyy: facepalm

thanks Tcobb, but i need to remove scripts from the returned data before i put it into the temp div. there is no any elements at that moment the data is just a large string. that's why i need a regular expression to remove the matching parts from the large string - something like this:

Code:

var noscripts=/bla-bla-bla/ig;
$.get("somepage.php",function(data){
data=data.replace(noscripts,'');/*this removes scripts from the data*/
$('#temp_div').html(data);
/*the data is clean for now and i can manipulate with it*/
});

xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"yyy: facepalm

I understand that the javascript may be self-executing, in which case my solution will not help you. I have had to do a similar thing in PHP, and here is the approximate process.

(1) Loop through the string, replacing every '<(space)' with '<'
(2) repeat the loop so long as the resultant length of the string is different
(3) do the same thing for '(space)>'
(4) use regexps to replace any variant of '<script such as '<SCript' with '<script'
(5) use regexps to replace any variant of '</script> such as '</SCript>' with '</script>'
(6) now use regexps to replace anything beginning with <script and ending with </script> with the empty string.

what i am trying to find is just regular expression for removing all the scripts with their tags from the response data markup before it is added on the main page... i could not compose such regexp by myself even using The Regex Coach that's why i asked for help ))

xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"yyy: facepalm

I changed your RegExp a bit so it would match more variations of script tags:

Code:

/<\s*script.*?>.*?(<\s*\/script.*?>|$)/ig

Though, I would choose rnd's solution over using regular expressions - it seems to the most secure way.

thanks man, but this regexp matches tags only and ignores everything between these tags. i do not need to remove or disable the script tags only, i am trying to find the way to cut off the script tags and all the code between them by replacing it with nothing

Oh wow, I forgot that the dot doesn't match newlines... This works for your example:

Code:

/<\s*script[^>]*>[\s\S]*?(<\s*\/script[^>]*>|$)/ig

And FYI, the last expression I posted did not ignore anything between the script tags - it only failed if the script contained newlines.

Edit: on a closer look, the biggest problem with your original expression is that you make a greedy search between the script's start and end tag, thus it'll match the very first <script>, the very last </script>, and everything in between (including other script end and start tags).

many thanks to all you guys who tried to help me in this thread! i've always knew that i could find help here ))

ReFreezed, the second regexp edition works perfectly - here is the evidence matches_now.png, thanks!Tcobb, i haven't tried your code yet, but i'm going to try it and put it in my "must have" js folder if it works (it looks like it does), thanks!rnd me, thank you for trying to help me, i very much appreciate it! i know that removing elements through the DOM would be the simpliest way, but it causes js-errors if i let these scripts stay in the response data, that's why i need string operations to sweep the scripts out of the data before i put it in the page. But anway, thanks!

xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"yyy: facepalm