1

I'm looking at http://asciitable.com

I'm trying to match all printable symbols, so the ranges would be the following (decimal):

33-47
58-64
91-96
123-126

The best I've seen so far is something like..

([!\"#$%&'()*+,\-.\/:;<=>?@[\\\]\^_`{\|}~"])+

Surely there's a better way?

In my application the regex will be partially visible to the user so I'd prefer something easier on the eyes (less backslashes to mess up and characters in general).

Edit: By symbols I mean everything in the range from 33 to 127 but excluding letters and numbers.

John Smith
  • 8,567
  • 13
  • 51
  • 74
  • No, but the question is badly phrased (by symbol, OP excludes letters and digits) – Aaron Jan 27 '18 at 22:47
  • @Aaron: Yeah not really sure what those are called my apologies. – John Smith Jan 27 '18 at 22:48
  • 3
    Usually, printable ASCII chars are matched with `[ -~]` – Wiktor Stribiżew Jan 27 '18 at 22:49
  • By *better way* are you trying to say *shorter*? – revo Jan 27 '18 at 23:11
  • 1
    Stepping back a bit, you could make such characters printable by using their representation from the [Control Pictures](http://www.fileformat.info/info/unicode/block/control_pictures/list.htm) block. Also, why are you focused on ASCII? JavaScript uses the UTF-16 encoding of the Unicode character set. Users might not understand "ASCII" when they are used to the full range of computerized characters – Tom Blodget Jan 28 '18 at 03:38

2 Answers2

2

The following character class should work fine :

[!-\/:-@\[-`{-~]

It's composed of the 4 mentioned ranges, where / and [ need to be escaped.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • Hm, I guess this is the best? You'd think there would be a more concise way of doing this. – John Smith Jan 27 '18 at 22:50
  • 1
    Using `(?![\w\s]).` would be more concise, but also less efficient. – Aaron Jan 27 '18 at 22:51
  • Oh, I should have mentioned that in the post. Efficiency does not matter at all for this application. Not sure which I like better. – John Smith Jan 27 '18 at 22:52
  • Well then I think I've got the same result with both regexs : https://regex101.com/r/5aKJIV/2/ vs https://regex101.com/r/InLTJ1/3 – Aaron Jan 27 '18 at 22:53
  • No it doesn't deal with underscores as it should, and if you want to deal with them then the regex becomes larger : `(?![A-Za-z0-9\s]).` or `(?![\w\s])[^_]` – Aaron Jan 27 '18 at 22:56
  • 2
    `(?![\w\s]).` is equal to `(?!\w)\S` however both match more than ascii symbols. – revo Jan 27 '18 at 23:03
2

Your current regex is perfectly fine, there is no better way really but if you are looking for a shorter one you may come with this:

(?=[!-~])[\W_]
revo
  • 47,783
  • 14
  • 74
  • 117