0

I am trying to remove some garbage js code on 4k+ webpages. The webpages have other js on the page as well so I need to only get rid of the JS with a particular function called clickIE.

I have this one liner working until I introduce the regex...

perl -p -i -e 's|<script(.*)>.*clickie.*?<\/script>|<\!--REMOVED-JS-CODE-->|gixsm' ./*.html

My regex works correctly when tested at http://www.gskinner.com/RegExr/ but fails on the command line...(fails meaning... the regex doesnt match anything)

3
  • Why are you using greedy instead of lazy matching? What specifically do you mean by "fails"? Commented Dec 18, 2011 at 7:47
  • @MarkByers your suggestion tightened it up. Thanks. Its working just fine when I test it at gskinner.com/RegExr. When I run it from the command line it doesn't do the replacement. perl -p -i -e 's|<script(.*?)>.*?clickie.*?<\/script>|<\!--REMOVED-JS-CODE-->|gixsm' ./*.html Commented Dec 18, 2011 at 8:05
  • This trips over <script>console.log("clickie")</script>. Admittedly, it's unlikely that you'll find 'clickie' in quotes anywhere in the sources you're looking at, but in the general case that may be less likely. Fixing this safely involves using a javascript parser to only find cases where the target string is being used as a function name, which is a somewhat harder problem. Commented Dec 19, 2011 at 11:03

2 Answers 2

3

For the regex to work, the entire file needs to be slurped in at once.

$ perl -0777 -pi -e 's/your/regex/gix' ./*.html
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Foiled me by 1 minute again, with the exact solution I was going to suggest. =P You may need to keep the flags, though.
@TLP : There is always one more way to do it ;)
0

As noted by @Mark the matches should be non-greedy. This seems to work...

perl -i -p -e 's|<script.*?>.*?clickie.*?</script>|<!-- removed -->|gism'

Comments

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.