Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

davedev100

macrumors member
Original poster
Jun 11, 2009
33
0
How would I create a regular expression to find and replace all the functions declarations in an class file with a copy of the function declaration and then a call to object passing the function name for debugging purposes.

For example, I'd want to replace

function showSkittles(){

with

function showSkittles(){
debugObject.traceFunction("showSkittles");


Pseudo expression code would look like this:


Find ("<>" enclose variables):

"public function" + <function name> + "}"

Replace with:

<found string> + line break + "debugObject. traceFunction(" + <function name> + ");"
 
Something like this?

"function\W*([^\(\W])\(\)\{\n"

"function $1(){\ndebugObject.traceFunction("$1");\n"
 
How would I create a regular expression to find and replace all the functions declarations in an class file with a copy of the function declaration and then a call to object passing the function name for debugging purposes.

For example, I'd want to replace

function showSkittles(){

with

function showSkittles(){
debugObject.traceFunction("showSkittles");

Don't do that. Look up in the compiler manuals what macros exist that evaluate to the function name. Like __func__ .
 
gnasher729, thanks for the suggestion. I'm working in ActionScript so the function name is not available. You can get a reference to the function but not the function name.

----------

Hansr, thanks for the regex. Can you give a brief explanation? It fails after "function\W*" for me and I don't know enough to figure it out.
 
Regex find and replace is implemented differently for each text editor/IDE but basically it's:


function <- the word function
\W* <- one or more whitespace characters
([^\(\W]) <- match anything not ( nor whitespace, e.g. function name
\(\) <- brackets ()
\{\n <- open curly brackets + linebreak

The replace works the same way but $1 will contain the match.
 
Hansr, thanks for the regex. Can you give a brief explanation? It fails after "function\W*" for me and I don't know enough to figure it out.

Based on Hansr's explanation, I would guess that the specification of whitespace characters is different for your regex processor.

Two other common ways of specifying a whitespace character are:
\S or \s
[:space:]

The latter is supposedly the POSIX standard, but sometimes you need extra square brackets on both sides because the regex processor treats square bracket characters specially too.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.