最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is this the shortest javascript regex to find all uppercase consonants? - Stack Overflow

programmeradmin2浏览0评论
/[B-DF-HJ-NP-TV-Z]/g

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

/[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.

Share Improve this question asked Mar 28, 2014 at 23:04 Keith GroutKeith Grout 9091 gold badge11 silver badges32 bronze badges 2
  • Your regex is already 16 characters. the slashes and g are not part of it. – aliteralmind Commented Mar 28, 2014 at 23:28
  • For this particular website they are. – Keith Grout Commented Mar 28, 2014 at 23:30
Add a comment  | 

4 Answers 4

Reset to default 9

16 char regex

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

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论