Sunday, July 13

To insert debug code in all functions using regular expression


Lets illustrate with a Javascript  Source Code and Notepad++ IDE

Regular Expression to Find functions

((\w*)\s*[:|=]\s*function\(\w*\)\s*\{)

Debug Code to insert inside each functions

console.log("functionName");

Regular Expression to Insert Debug Code inside Functions

$1 \n\tconsole\.log\("$2"\)\;

Note: Please enable "Regular expression" and ". matches newline" in Search Mode in Replace box in Notepad++ for inserting inside more than 1 function using regular expression.


A sample Javascript before inserting Debug code

var SyntaxHighlighter = function() {
    var sh = {
        all: function(params)
        {
            attachEvent(
                window,'description'
            );
        }
    };
}();

Javascript code after inserting Debug code using Regular Expression

var SyntaxHighlighter = function() {
      console.log("SyntaxHighlighter");
    var sh = {
        all: function(params)
        {
            console.log("all");
            attachEvent(
                window,'description'
            );
        }
    };
}();


To know more about How this Regular Expression Works and Customisation


No comments:

Post a Comment