1
+ // # 16 EXPRESIONES REGULARES
2
+ // # Monica Vaquerano
3
+ // # https://monicavaquerano.dev
4
+
5
+ /*
6
+ * EJERCICIO:
7
+ * Utilizando tu lenguaje, explora el concepto de expresiones regulares,
8
+ * creando una que sea capaz de encontrar y extraer todos los números
9
+ * de un texto.
10
+ *
11
+ * DIFICULTAD EXTRA (opcional):
12
+ * Crea 3 expresiones regulares (a tu criterio) capaces de:
13
+ * - Validar un email.
14
+ * - Validar un número de teléfono.
15
+ * - Validar una url.
16
+ */
17
+
18
+ // Regular Expresions
19
+
20
+ // Search Method
21
+ let text = "hola, mundo!" ;
22
+
23
+ // with a String
24
+ let x = text . search ( "mundo" ) ;
25
+ console . log ( x ) ;
26
+
27
+ // with a Regular Expression
28
+ x = text . search ( / m u n d o / i) ;
29
+ console . log ( x ) ;
30
+
31
+ x ? console . log ( "It's a match" ) : console . log ( "Not a match" ) ;
32
+
33
+ // Replace Method
34
+ text = "Visit Berlin!" ;
35
+
36
+ // with a String
37
+ x = text . replace ( "Berlin" , "Stuttgart" ) ;
38
+ console . log ( x ) ;
39
+
40
+ // with a Regular Expression
41
+ x = text . replace ( / B e r l i n / i, "Stuttgart" ) ;
42
+ console . log ( x ) ;
43
+
44
+ // test() Method
45
+ let pattern = / e / ;
46
+ x = pattern . test ( "The best things in life are free!" ) ;
47
+ x ? console . log ( "El patrón existe en la string" ) : console . log ( "El patrón NO existe en la string" )
48
+
49
+ // exec() Method
50
+ const obj = / e / . exec ( "The best things in life are free!" ) ;
51
+ console . log ( obj )
52
+ console . log ( `Found ${ obj [ 0 ] } in position ${ obj . index } in the text: ${ obj . input } ` )
53
+
54
+ // toString() Method
55
+ pattern = new RegExp ( "Hello World" , "g" ) ;
56
+ text = pattern . toString ( ) ;
57
+ console . log ( text )
58
+
59
+ // match() Method
60
+ const findDigits = ( txt ) => {
61
+ let pattern = / \d + / g;
62
+ let digits = txt . match ( pattern ) ;
63
+ return digits ;
64
+ }
65
+
66
+ console . log ( findDigits ( "The r4in in 5p4in" ) )
67
+
68
+ // Regular Expression Modifiers: Modifiers can be used to perform case-insensitive more global searches:
69
+ // Modifier Description
70
+ // i Perform case-insensitive matching
71
+ // g Perform a global match (find all)
72
+ // m Perform multiline matching
73
+ // d Perform start and end matching (New in ES2022)
74
+
75
+ // Extra
76
+
77
+ // E-Mail
78
+ const emailCheck = ( email ) => {
79
+ let pattern = / ^ [ \w . + - ] + @ [ \w . + - ] + \. [ a - z A - Z ] + $ / g;
80
+ let check = pattern . test ( email ) ;
81
+ return check ;
82
+ }
83
+ console . log ( "[email protected] es un email válido? =>" , emailCheck ( "[email protected] " ) ) // true
84
+ console . log ( "[email protected] es un email válido? =>" , emailCheck ( "[email protected] " ) ) // true
85
+ console . log ( "exampleexample.com es un email válido? =>" , emailCheck ( "exampleexample.com" ) ) // false
86
+ console . log ( "[email protected] es un email válido? =>" , emailCheck ( "[email protected] " ) ) // false
87
+
88
+ // # Número de teléfono
89
+ function telephoneCheck ( str ) {
90
+ let regex = / ^ ( 1 \s ? ) ? ( \( \d { 3 } \) | \d { 3 } ) ( [ \- \s ] ) ? ( \d { 3 } ) ( [ \- \s ] ) ? ( \d { 4 } ) $ / ;
91
+ let phoneCheck = regex . test ( str )
92
+ return phoneCheck ;
93
+ }
94
+
95
+ console . log ( "555-555-5555 es un número válido? =>" , telephoneCheck ( "555-555-5555" ) ) ; // true
96
+ console . log ( "1 (555) 555-5555 es un número válido? =>" , telephoneCheck ( "1 (555) 555-5555" ) ) ; // true
97
+ console . log ( "5555555555 es un número válido? =>" , telephoneCheck ( "5555555555" ) ) // true
98
+ console . log ( "(555)555-5555 es un número válido? =>" , telephoneCheck ( "(555)555-5555" ) ) // true
99
+ console . log ( "5555555 es un número válido? =>" , telephoneCheck ( "5555555" ) ) // false
100
+ console . log ( "123**&!!asdf# es un número válido? =>" , telephoneCheck ( "123**&!!asdf#" ) ) // false
101
+ console . log ( "11 555-555-5555 es un número válido? =>" , telephoneCheck ( "11 555-555-5555" ) ) // false
102
+ console . log ( "1 555)555-5555 es un número válido? =>" , telephoneCheck ( "1 555)555-5555" ) ) // false
103
+ console . log ( "(6054756961) es un número válido? =>" , telephoneCheck ( "(6054756961)" ) ) // false
104
+ console . log ( "27576227382 es un número válido? =>" , telephoneCheck ( "27576227382" ) ) // false
105
+ console . log ( "(275)76227382 es un número válido? =>" , telephoneCheck ( "(275)76227382" ) ) //false
106
+
107
+ // # URL
108
+ const urlCheck = ( url ) => {
109
+ let pattern = / ( h t t p s : \/ \/ w w w \. | h t t p : \/ \/ w w w \. | h t t p s : \/ \/ | h t t p : \/ \/ ) ? [ a - z A - Z ] { 2 , } ( \. [ a - z A - Z ] { 2 , } ) ( \. [ a - z A - Z ] { 2 , } ) ? \/ [ a - z A - Z 0 - 9 ] { 2 , } | ( ( h t t p s : \/ \/ w w w \. | h t t p : \/ \/ w w w \. | h t t p s : \/ \/ | h t t p : \/ \/ ) ? [ a - z A - Z ] { 2 , } ( \. [ a - z A - Z ] { 2 , } ) ( \. [ a - z A - Z ] { 2 , } ) ? ) | ( h t t p s : \/ \/ w w w \. | h t t p : \/ \/ w w w \. | h t t p s : \/ \/ | h t t p : \/ \/ ) ? [ a - z A - Z 0 - 9 ] { 2 , } \. [ a - z A - Z 0 - 9 ] { 2 , } \. [ a - z A - Z 0 - 9 ] { 2 , } ( \. [ a - z A - Z 0 - 9 ] { 2 , } ) ? / g;
110
+ let urlCheck = pattern . test ( url ) ;
111
+ return urlCheck ;
112
+ }
113
+
114
+ console . log ( "https://google.com/ es un url válido? =>" , urlCheck ( "https://google.com/" ) ) // true
115
+ console . log ( "http://google.com es un url válido? =>" , urlCheck ( "http://google.com" ) ) // true
116
+ console . log ( "https://www.google.com es un url válido? =>" , urlCheck ( "https://www.google.com" ) ) // true
117
+ console . log ( "www.google.com es un url válido? =>" , urlCheck ( "www.google.com" ) ) // true
118
+ console . log ( "google.com es un url válido? =>" , urlCheck ( "google.com" ) ) // true
119
+ console . log ( "google.c es un url válido? =>" , urlCheck ( "google.c" ) ) // false
120
+ console . log ( "goo/gle.c es un url válido? =>" , urlCheck ( "goo/gle.c" ) ) // false
121
+
122
+ // pattern = r"^(http[s]?://)?(www.)?[\w]+\.[\w]{2,}[/]?$"
0 commit comments