← Back to challenges

Regex Series: Initials

JavaScriptHardstringsregex

Instructions

Write a regular expression that checks if a string is a valid initial. Valid initials either look like (ex. for Chandler Muriel Bing):

  • C. B.
  • C. M. B.

Rules for a valid initial:

  1. Each letter must be upper case.
  2. Each letter must be immediately followed by a period.
  3. There must be exactly one space separating each letter-period pair.
  4. Leading or trailing whitespaces are valid.

Examples

"C. B." ➞ true

"    C. B." ➞ true
// Leading and trailing spaces are OK!

"C. B. k." ➞ false
// One of the initials is lower cased

"C B" ➞ false
// Missing a dot to immediately follow.

Notes

This challenge is designed to use RegEx only.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.