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

  1. Eric Windisch posted the following on May 12, 2006 at 12:32 pm.

    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. Calophi posted the following on May 12, 2006 at 12:59 pm.

    Thanks. Fixed it.

  3. Eric Windisch posted the following on May 12, 2006 at 1:09 pm.

    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

:mrgreen: :| :twisted: :arrow: 8O :) :? 8) :evil: :D :idea: :oops: :P :roll: ;) :cry: :o :lol: :x :( :!: :?:

  1. You will post the following soon.
    Go ahead and start typing.