$data =~ s{ # First, we'll list things we want # to match, but not throw away ( [^"'/]+ # other stuff | # -or- (?:"[^"\\]*(?:\\.[^"\\]*)*" [^"'/]*)+ # double quoted string | # -or- (?:'[^'\\]*(?:\\.[^'\\]*)*' [^"'/]*)+ # single quoted constant ) | # or we'll match a comment. Since it's not in the # $1 parentheses above, the comments will disappear # when we use $1 as the replacement text. / # (all comments start with a slash) (?: \*[^*]*\*+(?:[^/*][^*]*\*+)*/ # traditional C comments | # -or- /[^\n]* # C++ //-style comments ) }{$1}gsx; #### // Here are some comments var strText = "fee fi fo fum"; // More strText = strText.replace (/fee/i, "pee"); // More alert (strText); /* More */ #### // Here are some comments var strText = "My \"big\" example"; // More strText = strText.replace (/"/gi, "'"); // More alert (strText); /* More */ #### function BadQuoteTest (strInput) { strInput = strInput.replace(/"/gi, "'"); // aka aaa; /* This stuff is commented out so should be parsed out strInput = strInput.replace(/x/gi, "y"); // aka bbb; */ return (strInput); } function splitTest (strInput) { var pattern = /\s*;\s*/gi; return (strInput.split (pattern)); // Test } function splitTestWithLimit (strInput) { return (strInput.split (/\"/gi, 3)); // Test } function matchTest () { var strText = 'Cool text'; strText = strText.match (/oo/gi); // Test alert (strText); } function searchTest () { var strText = "Search text"; // Test strText = strText.search (/x/gi); // Test alert (strText); }