-1

For the below string,I want to select only the inner script tag containing the url http://cdn.walkme.com/users and replace the selected tag with an empty string so can somebody help me with the regex pattern

<script><script type="text/javascript">(function() {var walkme = document.createElement('script'); walkme.type = 'text/javascript'; walkme.async = true; walkme.src='http://cdn.walkme.com/users/cb643dab0d6f4c7cbc9d436e7c06f719/walkme_cb643dab0d6f4c7cbc9d436e7c06f719.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(walkme, s); window._walkmeConfig = {smartLoad:true}; })();</script></script>

I have tried this < script(.+)http://cdn.walkme.com/users/.+?\/script>

NetMage
  • 26,163
  • 3
  • 34
  • 55

1 Answers1

0

I agree that it's not really possible to have comprehensive and generic regex to parse any (x)HTML which standard supports. That's is true just by nature of these things.

But you're perfectly fine to do lots of smaller cool tasks using Regex. Just like in your case, in order to strip particular script out of the page markup, you could just use the following regex to find an entry and then replace it with an empty string:

\<script\>\<script type="text/javascript"\>\(function\(\) \{var walkme =.*\</script\>

It does very a simple thing - takes everything in between

<script><script type="text/javascript">(function() {var walkme = 

(you can include more text to be more specific) and

</script>

Just ensure special symbols (like /, ( or )) are escaped properly.

Edited In order to select inner need to use what is called positive lookahead to find first closing tag right after opening one:

<script type="text/javascript">\(function\(\) {var walkme =.*(?=</script>)