4 ms·
My favorite regex is /[ -~]*/, space-dash-tilde. That represents approximately A-Za-z0-9 plus punctuation and space. It's useful for something like `tr -d` to r
by web007 5y ago
My favorite regex is /[ -~]*/, space-dash-tilde. That represents approximately A-Za-z0-9 plus punctuation and space. It's useful for something like `tr -d` to remove all common characters and leave all of the "oddities" so you can differentiate "" from “” or ... from … in things like markdown or source code.
- O_H_E 5y agoOh! That's nice to know.
- zvr 5y agoAdd a tab character in there (before the space) and you're even better!
- chrismorgan 5y agoExplanation: [ -~] is matching the range of characters† from space (32) to tilde (126), which is the full range of printable ASCII characters. (0–31 are various control characters, and 127 is one last control character, ␡.) † “Characters” is a deliberately vague term here. Different regular expression engines match different types of things, typically supporting one or two modes. The three most common things to match are bytes (the traditional default, normally encoding-unaware but potentially known to be ASCII or UTF-8), UTF-16 code units (JavaScript’s default, and UTF-16 was an all-round terrible idea that ruined Unicode for everyone else while achieving nothing useful), and Unicode scalar values (which is typically called “Unicode mode” and often enabled by a /u flag).
- deleted 5y ago[deleted]