forked from hammadsaedi/regex-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.js
178 lines (147 loc) · 6.33 KB
/
factory.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
function createFormWithRegex(title, explanation, regexPattern ) {
// Create the form element
const form = document.createElement("form");
// Function to check the regular expression against the input text
function checkRegex() {
const userText = textInput.value;
const regexPattern = regexTextarea.value;
if (regexPattern.trim() !== "") {
const regex = new RegExp(regexPattern);
if (regex.test(userText)) {
textInput.classList.remove("invalid");
textInput.classList.add("valid");
} else {
textInput.classList.remove("valid");
textInput.classList.add("invalid");
}
}
}
// Add a title for the form
if (title) {
const formTitle = document.createElement("h3");
formTitle.textContent = title;
form.appendChild(formTitle);
}
// Add a paragraph for explaining the regular expression
if (explanation) {
const explanationPara = document.createElement("p");
explanationPara.textContent = explanation;
form.appendChild(explanationPara);
}
// Add a text input for user input
const textInput = document.createElement("input");
textInput.setAttribute("type", "text");
textInput.setAttribute("placeholder", "Enter text...");
textInput.classList.add("required"); // Added class for styling and validation
textInput.addEventListener("input", checkRegex); // Moved input event listener
// Add a textarea for regex input
const regexTextarea = document.createElement("textarea");
regexTextarea.setAttribute("placeholder", "Enter a regular expression...");
regexTextarea.value = regexPattern; // Set the initial regex pattern if provided
regexTextarea.addEventListener("input", checkRegex); // Moved input event listener
// Append the form elements to the form
form.appendChild(textInput);
form.appendChild(regexTextarea);
return form; // Return the form element
}
function createExtractionForm(title, explanation, regexPattern) {
// Create the form element
const form = document.createElement("form");
const unorderedList = document.createElement("ul");
// Function to extract content based on the provided regex
function addListItems(matches) {
console.log(matches);
unorderedList.innerHTML = ""; // Clear the existing list items
matches.forEach(match => {
const listItem = document.createElement("li");
listItem.textContent = match;
unorderedList.appendChild(listItem);
});
}
function extractContentWithRegex(text, regex) {
const regexObj = new RegExp(regex, "g");
let matches = [];
let match = regexObj.exec(String(text));
while (match !== null) {
matches.push(match[0]);
match = regexObj.exec(String(text));
}
return matches;
}
// Create a title for the form
if (title) {
const formTitle = document.createElement("h3");
formTitle.textContent = title;
form.appendChild(formTitle);
}
// Add a paragraph for explaining the regular expression
if (explanation) {
const explanationPara = document.createElement("p");
explanationPara.textContent = explanation;
form.appendChild(explanationPara);
}
// Add a text input for user input
const textInput = document.createElement("input");
textInput.setAttribute("type", "text");
textInput.setAttribute("placeholder", "Enter text...");
textInput.classList.add("required"); // Added class for styling and validation
textInput.addEventListener("input", function () {
addListItems(extractContentWithRegex(textInput.value, regexTextarea.textContent));
});
// Add a textarea for regex input
const regexTextarea = document.createElement("textarea");
regexTextarea.setAttribute("placeholder", "Enter a regular expression...");
regexTextarea.textContent = regexPattern; // Set the initial regex pattern if provided
regexTextarea.addEventListener("input", function () {
addListItems(extractContentWithRegex(textInput.value, regexTextarea.textContent));
});
// Append the form elements to the form
form.appendChild(textInput);
form.appendChild(regexTextarea);
form.appendChild(unorderedList);
return form;
}
function createSanitizationForm(title, explanation, regexPattern) {
// Create the form element
const form = document.createElement("form");
function sanitization(text, regex) {
const regexObj = new RegExp(regex, "g");
return text.replace(regexObj, '');
}
// Create a title for the form
if (title) {
const formTitle = document.createElement("h3");
formTitle.textContent = title;
form.appendChild(formTitle);
}
// Add a paragraph for explaining the regular expression
if (explanation) {
const explanationPara = document.createElement("p");
explanationPara.textContent = explanation;
form.appendChild(explanationPara);
}
// Add a textarea for sanitized input
const sanitizedTextarea = document.createElement("textarea");
sanitizedTextarea.setAttribute("readonly", "true");
// Add a text input for user input
const textInput = document.createElement("input");
textInput.setAttribute("type", "text");
textInput.setAttribute("placeholder", "Enter text...");
textInput.classList.add("required"); // Added class for styling and validation
textInput.addEventListener("input", function () {
sanitizedTextarea.value = sanitization(textInput.value, regexTextarea.textContent);
});
// Add a textarea for regex input
const regexTextarea = document.createElement("textarea");
regexTextarea.setAttribute("placeholder", "Enter a regular expression...");
regexTextarea.textContent = regexPattern; // Set the initial regex pattern if provided
regexTextarea.addEventListener("input",function () {
sanitizedTextarea.value = sanitization(textInput.value, regexTextarea.textContent);
});
// Append the form elements to the form
form.appendChild(textInput);
form.appendChild(regexTextarea);
form.appendChild(sanitizedTextarea);
return form;
}
export { createFormWithRegex, createExtractionForm, createSanitizationForm};