Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use regular expression to replace text in between <script> tags

Status
Not open for further replies.

avu

Technical User
Aug 8, 2003
53
CA
Hello,

How can I use regular expression to search and replace all text in between the '<script language="javascript">' and '</script>' tags?

Here's what I have:

function doReplace(myS) {
var regExp = /(<script)\/?[^>]+(</script>)/gi;
myS = myS.replace(regExp,"");
return myS;
}

where 'myS' would be the text containing the javascript.

I get error with searching for '(<script)' - it thinks it's nested scripts.

Thanks for your help.
 
you're pretty close...try this:

var regExp = /(\<script)\s*[^\>]*\>([^\<]*\<\/script>)?/gi;

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Thanks jeff, but it's still not quite.

Still get the error:
"Nested Script Block - A script block cannot be placed inside another script block."

Any other suggestions?

-allan

 
You probably have to break up the word "script".

var regExp = new RegExp("(\<scr"+"ipt)\s*[^\>]*\>([^\<]*\<\/scr"+"ipt>)?","gi");

Adam
 
Thanks Adam. Your method doesn't give error, but it only removes the tag '<script language="javascript">'. The rest is still intact.

Based on Jeff's suggestion, I also tried:
var regExp = /(\<scrip)\s*[^\>]*\>([^\<]*\<\/script>)?/gi;

-> (note of 'scrip') - and it also only removes the '<script language="javascript">' tag.

A temporary solution that (kinda) works is:
var regExp = /<\/?[^(<img)][^>]+>/gi;

-> this method will remove the text between the javascript tags (including the tags), but if you have any other tags in 'myS', it'll remove them as well. In this example, it will exclude '<img ...' tag. But if using this, you'll have add in a long list of other html tags that you want to exclude, which is very inefficient.

Any other suggestion?

Thanks again

-allan

 
works fine for me in IE and FF

adam is right though, you need to break up the word "script" if you use it in a var

Code:
<html>
<head>
<title>test</title>
<script type="text/javascript">

var s1 = '<span>foo</span><scr'+'ipt type="text/javascript">alert("foo");</scr'+'ipt><span>foo</span>';
var s2 = '<scr'+'ipt />';

function doReplace(myS) {
var regExp = /(\<script)\s*[^\>]*\>([^\<]*\<\/script>)?/gi;
myS = myS.replace(regExp,"");
return myS;
}

alert( doReplace(s1) );
alert( doReplace(s2) );
</script>
</head>

<body></body>
</html>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Yes, it does work fine in IE & FF. Though the limitation to this method is that the variable 'myS' is created in real-time, hence we can't break the word 'script' at run time.

I'll keep trying to find a work around. If anyone has other suggestions, please post.

Thanks guys,
-allan
 
Hi Jeff,

At run time, "myS" is assigned to content of another html page.

I'm trying to import an html page (say 'originalHtml') into another html page. The 'originalHtml' is fillout by users, and it may or may not contain <script> tag. In case it does, I'd like to remove all text in between this <script></script> tags.

Thanks.

-allan

 
hi, this worked fine for me:
Code:
<script>
TheString="asd<script>asdasdasdasdasd<\/script>asasdasd<script>d<\/script

>"
alert(TheString.replace(/<script[^>]*>.*?<\/script>/g,""))
</script>

Known is handfull, Unknown is worldfull
 
Hi vbkris,

Thanks, and that works fine, but how can we search & replace '</script>' with '<\/script>'? (of which comes back to the question that we started with) :)

-allan
 
not necessary, since i am typing the string i have to escape the /, but when the variable is assigned by javascript itself then it will take care of escaping...

Known is handfull, Unknown is worldfull
 
Thanks folks for all your help.

I've resolved to ASP to deal with the forementioned issue. I'll post the script if anyone interested (though this is not ASP forum :p).

Take care.

-allan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top