-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexwarrior.js
185 lines (157 loc) · 4.03 KB
/
regexwarrior.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright 2021. Eduardo Programador
www.eduardoprogramador.com
All Rights Reserved
This file contains routines
for regular expressions and text extractions.
1) Include it on your HTML File
Eg.: <stript src="js/regexwarrior.js"></script>
2) Initiate the class
Eg.: let reg = new RegexWarrior();
3)Invoke the methods
Eg.: reg.isEmail("[email protected]");
*/
class RegexWarrior {
/*
No argument required
*/
constructor() {
}
/*
Returns true if the param is a valid email.
False, otherwise
*/
isEmail(string) {
const email = /^[A-Za-z0-9-_]+[\.A-Za-z0-9-_]+@[A-Za-z0-9-_]{2,}\.[A-Za-z]{2,}\.*[A-Za-z]*/;
if(email.test(string)) {
return true;
} else {
return false;
}
}
/*
Returns true if the param is a valid phone number.
False, otherwise
*/
isPhoneNumber(string) {
const phone = /[\d]{7,15}/;
if(phone.test(string)) {
return true;
} else {
return false;
}
}
/*
Returns true if the param is a valid coordinate number.
False, otherwise
(latitude, longitude or altitude)
*/
isCoordinate(string) {
const coordinate = /-*\d{1,2}\.\d{7}/;
if(coordinate.test(string)) {
return true;
} else {
return false;
}
}
/*
For Brazilians only.
Returns true if the param is a valid CNPJ.
False, otherwise
*/
isCnpj(string) {
const cnpj = /\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}/;
if(cnpj.test(string)) {
return true;
} else {
return false;
}
}
/*
For Brazilians only
Returns true if the param is a valid cpf.
False, otherwise
*/
isCpf(string) {
const cpf = /\d{3}\.\d{3}\.\d{3}-\d{2}/;
if(cpf.test(string)) {
return true;
} else {
return false;
}
}
/*
For Brazilians Only.
Returns true if the param is a valid CEP.
False, otherwise
*/
isCep(string) {
const cep = /\d{5}-\d{3}/;
if(cep.test(string)) {
return true;
} else {
return false;
}
}
/*
Returns true if the param is a valid ip.
False, otherwise
*/
isIp(string) {
const ip = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
if(ip.test(string)) {
return true;
} else {
return false;
}
}
/*
Returns true if the param is a valid MAC Address.
False, otherwise
*/
isMacAddress(string) {
const mac = /^[A-Fa-f\d]{2}\.[A-Fa-f\d]{2}\.[A-Fa-f\d]{2}\.[A-Fa-f\d]{2}\.[A-Fa-f\d]{2}\.[A-Fa-f\d]{2}/;
if(mac.test(string)) {
return true;
} else {
return false;
}
}
/*
Extracts all numbers from a string
The result is an array
*/
extractNumbers(text) {
const number = /\d/;
var result = "";
var res = [];
while((result = number.test(text)) != null) {
res.push(result);
}
return res;
}
/*
Extracts all strings from a text.
The result is an array
*/
extractStrings(text) {
const string = /\w/;
var result = "";
var res = [];
while((result = string.test(text)) != null) {
res.push(result);
}
}
/*
Extracts all HTML tags from a text
*/
extractHtmlTags(text) {
const html = /<!*[*\w\s="-\/]+>/;
var result = "";
var res = [];
while((result = html.test(text)) != null) {
res.push(text);
}
}
}