Mar 03

How to write a safe catch-all RegExp

Tag: ChiliAndrea Ercolino @ 12:42:37

In my Chili recipes I use the regexp /(?:.|\n)/ when I want to mean each and every char. It cannot be just a dot, because a dot doesn’t match \n (AKA new line AKA line feed AKA 0×0A). Due to the fact that I erase from the input any \r (AKA carriage return AKA 0×0D) before trying a match, the regexp I use is almost safe, in fact I do not consider \u2028 nor \u2029, two additional (unicode) new line chars that a dot doesn’t match.

In short, and in general, a safe catch-all regexp is /(?:\w|\W)/ which means any word char or any non-word char.

So, if you are concerned with these unicode details and can’t wait till the next release of Chili, I advise you to search (?:.|\n) and replace any occurrence with (?:\w|\W).

Leave a Reply