The regular expression / [a-zA-Z]/g finds all the letters in the string. The trick is using a function as the replacement value in the call to the replace method. The switch case logic is trivial. It is just necessary to know that the first argument passed by replace to alpha2number is the matched string.
Note: These Regexs are examples and not built for a particular Regex engine. However, the PCRE syntax is mainly used. In particular, this means that character classes do not contain meta characters which need to be escaped, except the `-` and `]` character, where it is assumed that a `-` needs not to be escaped only when it is the last character in a character class.