Reg Ex for Script Tags

I thought it might be nice to have a little archive of Javascript Regular Expressions for other people coming through.

This one is for pulling <script> tags out of a string. It should match any case, account for attributes in the opening tag, and match any code including line breaks in between the tags.

First up, a code to jut match an opening <script> tag, because any open <script> tag can screw up a page, so you might want to take care of just that:

/<[Ss]*?script(s[Ss]*?)?>/gi

Next, a tag to grab a <script> tag from beginning to end, so that you can protect all the text, or replace it, or what have you.

/<[Ss]*?script(s[Ss]*?)?>[Ss]*?<[Ss]*?/script(s[Ss]*?)?>/gi
You can leave a response, or trackback from your own site.

3 Responses to “Reg Ex for Script Tags”

  1. Eric Windisch says:

    You need to escape , which you indicated the wordpress lost ;-)

    Anyway, to be through, you should use
    [\S\s]*?

    I believe this would be SGML/XHTML compliant, but I’m not exactly sure. I also haven’t tested this regex ;-)

  2. Eric Windisch says:

    Btw, my earlier comment got mungled by the wordpress gnomes as well. Blah!

    A few things for your readers:
    1. As many already know, you can take the second example and capture the javascript code with a group () — it will save the code into a variable.
    2. This is flexible for any HTML tag, just replace script with whatever you need.
    3. It won’t handle nested tags.

    #3 can be done by creating a recursive function, using the idea from #1. For all matched content, re-run the matching function. Return when there is no more matches within the content. Try this at home ;-)

Leave a Reply

You must be logged in to post a comment.

Subscribe to RSS Feed Follow me on Twitter!