2

Code:

$buffer = '
//Test
var url = \'http://test.com\';
var x = \'foo\'; // test
var myregex = \'/(*)?//\'';
echo preg_replace("/(?:\/\/.*)/", "", $buffer);

Result:

var url = 'http://
var x = 'foo'; 
var myregex = '/(*)?`

Expected Result:

var url = 'http://test.com';
var x = 'foo'; 
var myregex = '/(*)?//'

Is a first step to minify JavaScript, but im don´t see solution for this.

Any suggestion?

5
  • 5
    Is a first step to minify JavaScript : You know you're reinventing the wheel right? Commented May 1, 2015 at 14:50
  • 1
    It looks like your regex is doing what you've specified: remove // and everything following it from every line. Minification is not only a lot more than removing comments (eg. also reducing the size of private identifiers). For that you need to parse the language. But as it is a solved problem, why do you want to do this? Commented May 1, 2015 at 14:52
  • I could be completely off, but using the ? makes the .* lazy, not greedy, so it will match nothing, not everything. Commented May 1, 2015 at 14:54
  • 1
    @kainaw .*? would be lazy, the (?: ... .*) in the code is simply a non-stored sub-pattern. Commented May 1, 2015 at 14:59
  • I think you meant: uglifyjs --compress --mangle -- input.js Commented May 1, 2015 at 15:52

1 Answer 1

1

Doing this with RegEx would be very hard.

With RegEx:

  • You should use lookbehind assertions to check if the // was not inside a string sequence by testing for ' or ".
  • There could also be multiple strings on one line, like "http://", "https://", so you would need nested assertions.
  • Strings could be enclosed in ' or ", so you should find a way to test this: "http://", 'https://'

The other option

The other option and most suitable solution is to go through the string character by character and keep a set of boolean variables to track the "state" of the code string. For example, keep track of being inside a string: if ($char == '"') { $inString = true; }.

Above is a simplistic example. When you are going to do this for real, you're probably best off writing OOP code to parse it and pass the handled string and current character to different objects handling different types of code. This is too broad to explain it here in full..

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

Comments

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.