/* * ZipCode オブジェクトを生成します。 * * 郵便番号として受け入れられる書式は次のとおりです。 * 12345 * 12345-6789 * 123456789 * 12345 6789 * * もし ZipCode コンストラクターに渡された引数が、これらのパターンの * うちのどれにも一致しないのであれば、例外が発生します。 */ class ZipCode { static pattern = /[0-9]{5}([- ]?[0-9]{4})?/; constructor(zip) { zip = String(zip); const match = zip.match(ZipCode.pattern); if (!match) { throw new ZipCodeFormatException(zip); } // 郵便番号の値は文字列中で最初に一致した部分です
![throw - JavaScript | MDN](https://cdn-ak-scissors.b.st-hatena.com/image/square/aa767575b4232d312d2c5de428f0bf0955805e26/height=288;version=1;width=512/https%3A%2F%2Fdeveloper.mozilla.org%2Fmdn-social-share.cd6c4a5a.png)