Match Blank Characters outside Multiline Comments

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
 
L

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:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top