5
/[B-DF-HJ-NP-TV-Z]/g

This is 20 total characters. http://regex101.com/quiz/# quiz #3 says that the shortest solution is 16 characters, but I'm not sure if that is for the JavaScript flavor of regexes.

Keith Grout
  • 899
  • 11
  • 30

4 Answers4

7

16 char regex

(?![AEIOU])[A-Z]

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
7

this is Only ASCII, so this will the shortest regex, use Negated Character Classes, please see Negated Character Classes

[^ -AEIOU[-ÿ], 13 characters

/[^ -AEIOU[-ÿ]/g, with flag, 16 characters

lautumn
  • 96
  • 1
  • 3
  • Doesn't work anymore `Test 5/10: Your regex is matching characters in the range 0-31, and I'm quite certain those are not consonants!` – Danny Lo Oct 18 '21 at 07:57
  • @DannyLo If you can copy "[^-AEIOU[-ÿ]" (show as "[^U+0001-AEIOU[-ÿ]" on regex101.com), copy it, or generate it with bash. I use `echo -en '[^\x01-AEIOU[-ÿ]' | pbcopy ` on macos to copy. – liudongmiao Jul 01 '22 at 05:55
  • @liudongmiao ah, so that's not a whitespace... not it makes sense, thanks – Danny Lo Jul 02 '22 at 09:35
4

You can get it a little bit shorter by using a \P{Lu} class:

[^\P{Lu}AEIOU]

[I'm not restricting this to Javascript because regex101 is primarily PCRE flavour]

The above has 14 characters. Since the puzzle also adds in characters from the word boundaries and flags, this adds 3 more characters for //g, hence total 17 characters.

In .NET, you can do it shorter:

[B-Z-[EIOU]]

(12 chars long)

For javascript:

(?![EIOU])[B-Z]

15 chars excluding delimiters and flag.

Jerry
  • 70,495
  • 13
  • 100
  • 144
  • Thanks! I'm not familiar with that notation. Anywhere I can read a little more about it and does Javascript support it? – Keith Grout Mar 28 '14 at 23:14
  • @KeithGrout Sorry, JS doesn't support that class. You can read more about them [here](http://perldoc.perl.org/perlunicode.html#Unicode-Character-Properties). – Jerry Mar 28 '14 at 23:17
  • Awesome. Your solution *is* the shortest solution per regex101 (where I mistakenly thought I was using javascript). Gotta accept the other answer since it answers exactly what I asked... No hard feelings eh? – Keith Grout Mar 28 '14 at 23:31
  • 1
    @KeithGrout Huh didn't think about a shorter regex for JS until now. Edited answer =P – Jerry Mar 29 '14 at 00:10
  • 1
    Thank you, I learned from this answer about `\p` and `\P`. But wait.. why is it the accepted one? `/[^\P{Lu}AEIOU]/g ` are 17 chars, not 16... – Danny Lo Oct 14 '21 at 22:26
  • @DannyLo That's just what the OP marked at the time... lautumn added their answer 5 years after the OP asked the question (the shortest I guess at the time of this comment) and OP is probably not around that much anymore to change it. – Jerry Oct 18 '21 at 05:44
3

My regex is 17 characters in length! I am still one shorter!

(?=[A-Z])[^AEIOU]

Using lookahead it is first checking the next character is in between A-Z or not. Then its checking for a Non-Vowel character.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85