php - How can I de-comment JavaScript code with this preg_replace? - Stack Overflow

I'm trying to dement myments in my javascript with php preg_replace() and made a preg_replace

I'm trying to dement my // ments in my javascript with php preg_replace() and made a preg_replace which should do following:

1.When a ment start on a new line, delete that entire line: // COMMENTS .....

2.When ment is halfway behind a script, after 1 TAB // remove that ment part exampleScript(); // (1space) ments

3.Don't match the // in http://

This pregreplace does the above job, HOWEVER, it currently removes 3 lines of code with // in it. (see the false matches header below) which it should skip.

$buffer = preg_replace('/(?<!http:)\/\/\s*[^\r\n]*/', '', $buffer);

good matches

//something

// something *!&~@#^hjksdhaf

function();// ment

false matches

(/\/\.\//)
"//"  
"://"  

So, How can I filter these three false matches out and how to change the below regex?

(?<!http:)\/\/\s*[^\r\n]*

PS, I don't wish to use others' code minifiers/frameworks with their own overheads. Just my own for now.

I'm trying to dement my // ments in my javascript with php preg_replace() and made a preg_replace which should do following:

1.When a ment start on a new line, delete that entire line: // COMMENTS .....

2.When ment is halfway behind a script, after 1 TAB // remove that ment part exampleScript(); // (1space) ments

3.Don't match the // in http://

This pregreplace does the above job, HOWEVER, it currently removes 3 lines of code with // in it. (see the false matches header below) which it should skip.

$buffer = preg_replace('/(?<!http:)\/\/\s*[^\r\n]*/', '', $buffer);

good matches

//something

// something *!&~@#^hjksdhaf

function();// ment

false matches

(/\/\.\//)
"//"  
"://"  

So, How can I filter these three false matches out and how to change the below regex?

(?<!http:)\/\/\s*[^\r\n]*

PS, I don't wish to use others' code minifiers/frameworks with their own overheads. Just my own for now.

Share Improve this question edited Mar 2, 2011 at 3:26 Sam asked Mar 2, 2011 at 3:01 SamSam 15.5k25 gold badges96 silver badges153 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Why not use a preexisting JavaScript minifier, like the YUI Compressor (PHP bindings here)?


If you are really set on writing your own, have a look through the source code to see how it's done.
Short version: The Right Way is to use a proper parser/tokenizer approach.

The grammar of JavaScript is a context-free grammar (I believe it's LL(1)-parseable). It cannot be parsed with regular expressions.

In the theory of formal languages in putability theory, there is a result known as the pumping lemma which proves that you cannot parse arbitrary context-free grammars with a regular expression.

The gist of the problem is this: you can't just look for the string //, because it could be contained inside otherwise valid code, for example, a string. You can't just look for a // inside two quotation marks, because then you'd get false positives like alert('no!') // can't do it where the text ) // can is technically contained between two ' marks. Instead, you'd have to detect where strings begin and end. Worse, one type of strings can be nested inside another type of strings, and strings (even half-open strings) can be nested inside of ments!

There is no simple general solution -- JavaScript syntactic elements like strings, brackets, parentheses, etc., can be nested arbitrarily many levels deep. The only way to accurately detect where any syntactic element begins and ends is to correctly parse all the syntactic elements that you might encounter along the way.

The correct answer is to use an actual parser.

$buffer = preg_replace('/(?<!\S)\/\/\s*[^\r\n]*/', '', $buffer);

Works on all of the instances mentioned in the question: keeps the positive matches, removes the false matches.

Three awesome websites on the net that help with finding the correct regex:

http://gskinner./RegExr/

http://lumadis.be/regex/test_regex.php

http://cs.union.edu/~hannayd/csc350/simulators/RegExp/reg.htm

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742310043a4419708.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信