Match Blank Characters outside Multiline Comments

  • Thread starter Thread starter Laser Lu
  • Start date Start date
L

Laser Lu

Hi, all,
I'm now writing a program to compress JavaScript code. One puzzle is how
to write a regular expression to find out and remove all the redundent blank
spaces. However, those blank spaces that are in the comments should be kept
intact.

I've tried to write some Regexs, and I list them here for your information:
regex = new Regex(@"/\*[\s\S]*?\*/"); // pattern used to match a multiline
comment block
regex = new Regex(@"//[^@\n]*\n"); // pattern used to match a single-line
comment
regex = new Regex(@"(?<!(?://[^\n]*))\s+"); // pattern used to match blank
characters outside single-line comments

Now the problem is how to match all the continuous blank characters outside
multiline comments? I tried the following Regex:
regex = new Regex(@"(?<!(?:/\*[\s\S]*))\s+");
but this does not work, and will lead to a CPU dead-loop with no reactions
for a long time.

I'm a newbie in Regular Expression. Can sombody help me? Thanks a lot!

Best regards,
Laser Lu
 
I wrote another one, althogh it seems a little bit longer:)

(?<=(?:\*/)(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*(?:/\*))|(?<=\A(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*(?:/\*))|(?<=(?:\*/)(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*\z)|(?<=\A(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*\z)


I did some simple test, and it works:)
 
Back
Top