|
| 1 | +/* |
| 2 | + Regular expressions... |
| 3 | +*/ |
| 4 | + |
| 5 | +console.log('Regular expressions...') |
| 6 | + |
| 7 | +const customText: string = |
| 8 | + '¡Hola Mundo! Hoy es 15/04/2024. Quedan 263 días para terminar el año 2024.' |
| 9 | + |
| 10 | +const numbersInsideCustomText: `${number}` = customText |
| 11 | + .match(/[0-9]/g) |
| 12 | + ?.join('') as `${number}` |
| 13 | + |
| 14 | +console.log(`\n\`customText\` = '${customText}'`) |
| 15 | + |
| 16 | +console.log(`\nNumbers inside \`customText\` --> ${numbersInsideCustomText}`) |
| 17 | + |
| 18 | +console.log( |
| 19 | + '\n# ---------------------------------------------------------------------------------- #\n' |
| 20 | +) |
| 21 | + |
| 22 | +/* |
| 23 | + Additional challenge... |
| 24 | +*/ |
| 25 | + |
| 26 | +console.log('Additional challenge...') |
| 27 | + |
| 28 | +const regularExpressions = { |
| 29 | + email: new RegExp(/^[a-zA-Z0-9]*@(gmail|outlook|hotmail|yahoo)\.com$/), |
| 30 | + phoneNumber: new RegExp(/^\+\d{1,4} \d{10}$/), |
| 31 | + url: new RegExp( |
| 32 | + /^https?:\/\/([a-zA-Z0-9]*\.)?[a-zA-Z0-9]*\.([a-zA-Z]{3})(\.[a-zA-Z]{2,3})?\/?$/ |
| 33 | + ), |
| 34 | +} |
| 35 | + |
| 36 | +const emails: string[] = [ |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +] |
| 43 | + |
| 44 | +const phoneNumbers: string[] = [ |
| 45 | + '+1234567890', |
| 46 | + '+1 1234567890', |
| 47 | + '+1234 1234567890', |
| 48 | + '+123456789', |
| 49 | + '+123456789 1234567890', |
| 50 | +] |
| 51 | + |
| 52 | +const urls: string[] = [ |
| 53 | + 'https://www.example.cóm', |
| 54 | + 'http://example.com', |
| 55 | + 'https://subdomain.example.com', |
| 56 | + 'http://www.example.c2.uk', |
| 57 | + 'https://www.example.org', |
| 58 | +] |
| 59 | + |
| 60 | +console.log('\n\nEmails...') |
| 61 | + |
| 62 | +for (const email of emails) { |
| 63 | + const isValid: boolean = regularExpressions.email.test(email) |
| 64 | + console.log(`\n'${email}' is valid? --> ${isValid}`) |
| 65 | +} |
| 66 | + |
| 67 | +console.log('\n\nPhone numbers...') |
| 68 | + |
| 69 | +for (const phoneNumber of phoneNumbers) { |
| 70 | + const isValid: boolean = regularExpressions.phoneNumber.test(phoneNumber) |
| 71 | + console.log(`\n'${phoneNumber}' is valid? --> ${isValid}`) |
| 72 | +} |
| 73 | + |
| 74 | +console.log('\n\nUrls...') |
| 75 | + |
| 76 | +for (const url of urls) { |
| 77 | + const isValid: boolean = regularExpressions.url.test(url) |
| 78 | + console.log(`\n'${url}' is valid? --> ${isValid}`) |
| 79 | +} |
0 commit comments