0

I want to remove a specific function which is void (doesn't return anything) from my javascript source code using php at run time. Consider the following js code which is supposed to be outputted by php:

var a = func1(x1);
func2(a + ": " + x1);
// other codes
func2(someOther("Lol") + x1);
// other codes

here I want to remove func2 (it is something like a log function which outputs something to js console).

Now first thing that came to my mind was writing a regex and replace it with an empty string using preg_replace. But I wasn't successful in defining the right expression. I was wondering that is it possible to write a regex to match the function calls? If it is could you please give me some hints or examples so that I could use it?

And if you think that there is an alternative way of doing it, I'd appreciate to hear yours.

Notes

  • The function is not used in complicated structures like if conditions and etc. (I mean you don't need to consider these situations)
  • It is possible to call other functions inside this one (as arguments)
  • I can not change js codes. (they are submitted by other people)
2
  • 3
    There's no need to do this. Simply add JavaScript code to overwrite the function definition. Commented Nov 21, 2013 at 22:07
  • So, you want to remove the function calls, not the function itself. You might want to update the title. Apart from that: +1 for redefining the function: func2 = function (a) {}; Commented Nov 21, 2013 at 22:15

2 Answers 2

2

If you want the global function "func2" to go completely away, you can just make sure that your page(s) end with:

<script> window.func2 = undefined; </script>

If there's code that depends on the function existing, you can do:

<script> window.func2 = function() {}; </script>

Now, that assumes that the function is defined in some normal way, and that code you drop at the end of the body will run after the code that defines the function originally has run. If the function is defined by something more dynamic, then things get more complicated.

In any case, trying to write code that "edits" some JavaScript code is going to end up being either extremely fragile, or a project involving a complete JavaScript parser. Something sounds dysfunctional about a server application that has arguments with itself.

Sign up to request clarification or add additional context in comments.

1 Comment

What a clean way! Thank for your innovative solution.
2

A basic regex to do this is :

$result = preg_replace('~\bfunc2\s*(\((?>[^()]++|(?1))*\));?~', '', $code);

However: it doesn't deal with parenthesis inside quotes. To do that you must use:

~\bfunc2\s*(\((?>[^()"']++|"(?>[^"\\]++|\\{2}|\\.)*"|'(?>[^'\\]++|\\{2}|\\.)*'|(?1))*\));?~s

as pattern (don't forget to escape the quote you use).

1 Comment

Thank you for your solution mate. I tested the first expression and it worked (as you said it didn't work for parentheses inside quotes)

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.