Skip to content

Commit faa21b1

Browse files
authored
Merge pull request mouredev#3985 from nlarrea/js-reto16
#16 - javascript
2 parents 8f72b16 + a09447b commit faa21b1

File tree

1 file changed

+60
-0
lines changed
  • Roadmap/16 - EXPRESIONES REGULARES/javascript

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* EJERCICIO:
3+
* Utilizando tu lenguaje, explora el concepto de expresiones regulares,
4+
* creando una que sea capaz de encontrar y extraer todos los números
5+
* de un texto.
6+
*
7+
* DIFICULTAD EXTRA (opcional):
8+
* Crea 3 expresiones regulares (a tu criterio) capaces de:
9+
* - Validar un email.
10+
* - Validar un número de teléfono.
11+
* - Validar una url.
12+
*/
13+
14+
const text = `
15+
Lorem ipsum dolor1 sit amet, consectetur adipiscing elit. Nullam ac mi in
16+
ipsum ultricies 2 varius. Nullam euismod, justo nec fermentum ultricies, nunc
17+
nisl tempus purus, 3 sed dictum 563 nulla odio nec purus. Nullam nec purus
18+
consectetur, consectetur justo. 10 Nullam nec purus consectetur, consectetur.
19+
`
20+
21+
const numbersPattern = /[\d]{1,}/g;
22+
const numbers = text.match(numbersPattern);
23+
console.log(numbers);
24+
// ['1', '2', '3', '563', '10']
25+
26+
27+
/*
28+
* DIFICULTAD EXTRA (opcional):
29+
* Crea 3 expresiones regulares (a tu criterio) capaces de:
30+
* - Validar un email.
31+
* - Validar un número de teléfono.
32+
* - Validar una url.
33+
*/
34+
35+
36+
function printTest(pattern, text) {
37+
console.log(text + ':', text.match(pattern)?.length > 0);
38+
}
39+
40+
41+
console.log('\nTesting emails:');
42+
const emailPattern = /^[\w]{1,}[\w\d\._]{4,}\@[\w]{2,}\.[\w]{2,3}$/g;
43+
printTest(emailPattern, '[email protected]'); // true
44+
printTest(emailPattern, '[email protected]'); // false
45+
printTest(emailPattern, 'an_email1234'); // false
46+
printTest(emailPattern, '[email protected]'); // true
47+
printTest(emailPattern, 'another.2024@gmail'); // false
48+
printTest(emailPattern, 'another.2024@gmail.'); // false
49+
printTest(emailPattern, '[email protected]'); // true
50+
51+
console.log('\nTesting phone numbers:');
52+
const phonePattern = /^\+?\d{3,}$/g;
53+
printTest(phonePattern, '123456789'); // true
54+
printTest(phonePattern, '+34123456789'); // true
55+
printTest(phonePattern, 'abcdefghi'); // false
56+
57+
console.log('\nTesting URLs:');
58+
const urlPattern = /^https?:\/\/(w{3}\.)?[\w\d\.\-\?]+\.[\w]{2,}\/?$/g
59+
printTest(urlPattern, 'https://github.com/'); // true
60+
printTest(urlPattern, 'https://www.twitch.tv/'); // true

0 commit comments

Comments
 (0)