|
3 | 3 | * Covers common formats across English, Chinese, Japanese, Korean. |
4 | 4 | */ |
5 | 5 | export function extractCode(text: string): string | null { |
| 6 | + // Ordered by specificity: try numeric-first patterns, then alphanumeric. |
| 7 | + // Rationale: real verification codes are almost always numeric. Matching |
| 8 | + // alphanumeric first causes false positives like "Your" being captured |
| 9 | + // after "verification code " (where the next token is a word, not a code). |
6 | 10 | const patterns = [ |
7 | | - // "验证码:123456" / "verification code: 123456" / "認証コード:123456" / "인증 코드: 123456" |
8 | | - /(?:验证码|verification\s*code|認証コード|确认码|confirm(?:ation)?\s*code|security\s*code|passcode|OTP|pin\s*code|인증\s*코드|코드)[:\s:\-]+([A-Za-z0-9]{4,8})/i, |
9 | | - // "code is 123456" / "code: 123456" |
10 | | - /\bcode\s*(?:is|:)\s*([A-Za-z0-9]{4,8})/i, |
| 11 | + // "code is 123456" / "code: 123456" — numeric only |
| 12 | + /\bcode\s*(?:is|:)\s*(\d{4,8})\b/i, |
| 13 | + // "验证码:123456" / "verification code: 123456" — numeric only, require explicit delimiter (colon, 是/为/is) |
| 14 | + /(?:验证码|verification\s*code|認証コード|确认码|confirm(?:ation)?\s*code|security\s*code|passcode|OTP|pin\s*code|인증\s*코드|코드)\s*(?:[::]|is|\bis\b|为|是)\s*(\d{4,8})\b/i, |
| 15 | + // Alphanumeric with explicit "is" or colon delimiter |
| 16 | + /\bcode\s*(?:is|:)\s*([A-Za-z0-9]{4,8})\b/i, |
| 17 | + /(?:验证码|verification\s*code|認証コード|确认码|confirm(?:ation)?\s*code|security\s*code|passcode|OTP|pin\s*code|인증\s*코드|코드)\s*(?:[::]|is|为|是)\s*([A-Za-z0-9]{4,8})\b/i, |
11 | 18 | // Standalone 4-8 digit number (surrounded by whitespace/boundaries) |
12 | 19 | /(?:^|\s)(\d{4,8})(?:\s|$|\.|,)/m, |
13 | 20 | ] |
|
0 commit comments