0

Does someone know how to check if a code begins by <script> and end by </script>. It's just a means to check if code is ok to be inserted in HTML to avoid breaking the site.

So far I've tried to look if <script> is there but I need to check both opening and closing.

Any help would be cool :)

6
  • 1
    It's the easiest regex to write but it won't check your string is valid. Parse the HTML for that or do additional check. Commented Sep 17, 2013 at 13:03
  • Thanks dystroy, I have to admit regex are tricky for me. Commented Sep 17, 2013 at 13:07
  • Whoever attempts to answer this question, please take into account that <script>....</script>.....</script> is invalid. Commented Sep 17, 2013 at 13:11
  • invalid regarding what? Commented Sep 17, 2013 at 13:14
  • Aren't you worried that script itself will break the site? Commented Sep 17, 2013 at 13:15

1 Answer 1

5

You could use this..

<script[^>]*>([\s\S]*?)<\/script>
Sign up to request clarification or add additional context in comments.

2 Comments

Almost correct... except that this doesn't handle double closing tags.
@JMau: You didn’t surround it with /. Also, what is this for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.