Skip to content

Commit b87b1a2

Browse files
committed
feat: Add support for various regex operations in getValue function
- Expanded getValue function to handle additional regex operations: split, lastIndex, search, and exec. - Added value-split attribute to split the element's value based on a regex. - Added value-lastindex attribute to return the index at which to start the next match. - Added value-search attribute to return the index of the first match of the regex in the value. - Added value-exec attribute to execute a search for a match and return detailed match information. - Updated regexParser function to parse and handle these new regex operations. - Enhanced error handling and logging for debugging purposes. This update makes the getValue function more versatile and capable of handling a comprehensive range of regex operations.
1 parent 8ebfbd6 commit b87b1a2

File tree

1 file changed

+90
-8
lines changed

1 file changed

+90
-8
lines changed

src/getValue.js

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,31 +178,113 @@ const getValue = (element) => {
178178
}
179179
}
180180

181+
// try {
182+
// let replace = element.getAttribute('value-replace');
183+
// let replaceAll = element.getAttribute('value-replaceall');
184+
185+
// if (replace || replaceAll) {
186+
// let { regex, replacement } = regexParser(replace || replaceAll)
187+
// if (regex) {
188+
// if (replace)
189+
// replace = regex
190+
// else if (replaceAll)
191+
// replaceAll = regex
192+
// }
193+
194+
195+
// replacement = replacement || element.getAttribute('value-replacement') || "";
196+
197+
// if (replacement !== undefined) {
198+
// if (replace)
199+
// value = value.replace(replace, replacement);
200+
// else
201+
// value = value.replaceAll(replaceAll, replacement);
202+
// }
203+
// }
204+
// } catch (error) {
205+
// console.error('getValue() replace error:', error, element);
206+
// }
207+
181208
try {
209+
let value = element.value; // or another way to get the element's value
210+
182211
let replace = element.getAttribute('value-replace');
183212
let replaceAll = element.getAttribute('value-replaceall');
213+
let test = element.getAttribute('value-test');
214+
let match = element.getAttribute('value-match');
215+
let split = element.getAttribute('value-split');
216+
let lastIndex = element.getAttribute('value-lastindex');
217+
let search = element.getAttribute('value-search');
218+
let exec = element.getAttribute('value-exec');
219+
220+
if (replace || replaceAll || test || match || split || lastIndex || search || exec) {
221+
let { regex, replacement } = regexParser(replace || replaceAll || test || match || split || lastIndex || search || exec);
184222

185-
if (replace || replaceAll) {
186-
let { regex, replacement } = regexParser(replace || replaceAll)
187223
if (regex) {
188224
if (replace)
189-
replace = regex
225+
replace = regex;
190226
else if (replaceAll)
191-
replaceAll = regex
227+
replaceAll = regex;
228+
else if (test)
229+
test = regex;
230+
else if (match)
231+
match = regex;
232+
else if (split)
233+
split = regex;
234+
else if (lastIndex)
235+
lastIndex = regex;
236+
else if (search)
237+
search = regex;
238+
else if (exec)
239+
exec = regex;
192240
}
193241

194-
195242
replacement = replacement || element.getAttribute('value-replacement') || "";
196243

197244
if (replacement !== undefined) {
198-
if (replace)
245+
if (replace) {
199246
value = value.replace(replace, replacement);
200-
else
247+
} else if (replaceAll) {
201248
value = value.replaceAll(replaceAll, replacement);
249+
}
250+
}
251+
252+
if (test) {
253+
value = regex.test(value);
254+
}
255+
256+
if (match) {
257+
const matches = value.match(match);
258+
if (matches) {
259+
value = matches[1] || matches[0]; // prioritize capturing group match if available
260+
}
261+
}
262+
263+
if (split) {
264+
value = value.split(split);
265+
}
266+
267+
if (lastIndex) {
268+
regex.lastIndex = 0; // Ensure starting index is 0
269+
regex.test(value);
270+
value = regex.lastIndex;
271+
}
272+
273+
if (search) {
274+
value = value.search(search);
275+
}
276+
277+
if (exec) {
278+
const execResult = regex.exec(value);
279+
if (execResult) {
280+
value = execResult[1] || execResult[0]; // prioritize capturing group match if available
281+
} else {
282+
value = null;
283+
}
202284
}
203285
}
204286
} catch (error) {
205-
console.error('getValue() replace error:', error, element);
287+
console.error('getValue() error:', error, element);
206288
}
207289

208290
let lowercase = element.getAttribute('value-lowercase')

0 commit comments

Comments
 (0)