chalk / ansi-regex

Regular expression for matching ANSI escape codes
MIT License
184 stars 79 forks source link

Not matching certain ANSI escape codes #1

Closed arjunmehta closed 10 years ago

arjunmehta commented 10 years ago

Hi there, I'm using your strip-ansi module, and I understand it is using this module to find ANSI codes to strip.

I encountered a problem where a code used by mocha, \u001b[0G is not being stripped/found using your regex.

I see the regex is /\u001b\[([0-9]{1,3}(;[0-9]{1,3})*)?[m|K]/g;. I wonder if adding a |G might help with this particular issue, but I think there are a whole plethora of other codes being missed by this. I am not sure which ANSI escape codes you would want to support, but here is a pretty comprehensive list of VT100 codes: http://ascii-table.com/ansi-escape-sequences-vt-100.php

I can help if you'd like!

Thanks! Arjun

sindresorhus commented 10 years ago

@arjunmehta hah, coincidence. I noticed this just today too that there's a LOT of various ways to create ansi escape codes. I should have looked it up sooner and not just gone by assumption.

Any help welcome :)

arjunmehta commented 10 years ago

@sindresorhus That's funny! Yes there are many different ANSI codes and there are likely a few ways of dealing with this.

I asked a question on Stack Overflow about an algorithmic solution yesterday as I was trying to find an effective way of dealing with the problem. I'm not the best at asking Stack Overflow questions and it got downvoted for some reason, but there were a couple really great answers which came out of it which I tweaked a bit. One of the authors of an answer started a jsperf which I tweaked a bit.

Regex is a really slow way of doing this (and so the name of the module may have locked in the solution :P) but looping through the string is more efficient, apparently.

The other way is to create a table of characters/character codes and deal with them that way.

arjunmehta commented 10 years ago

@sindresorhus I found a much better regex to use.

I'm working on some scripts that help test/reveal which of the listed VT100 codes the regex will match, and which ones won't match.

I'll send a pull request that includes the new regex and additions to the test which ought to help test in the future for other codes.