new function for regex execution

This commit is contained in:
Timo Hocker 2020-03-30 11:00:52 +02:00
parent 9f1022ff18
commit d849b45223
2 changed files with 26 additions and 1 deletions

View File

@ -17,4 +17,11 @@ const obj = {foo:'bar'};
const copy = util.copy_object(obj); const copy = util.copy_object(obj);
copy.foo = 'baz'; copy.foo = 'baz';
console.log(obj.foo); // bar console.log(obj.foo); // bar
// run a regular expression and get a callback for every result
const data = "foobarfoo";
const regex = /foo/g;
util.run_regex(regex, data, res => {
console.log(res[0]); // will output 'foo' 2 times
});
``` ```

View File

@ -45,8 +45,26 @@ function copy_object (obj) {
return JSON.parse (JSON.stringify (obj)); return JSON.parse (JSON.stringify (obj));
} }
/**
* run a regular expression and callback for every result
*
* @param regex regular expression
* @param data data to run on
* @param func function to execute
*/
function run_regex (regex, data, func) {
let res = regex.exec (data);
while (res) {
func (res);
res = regex.exec (data);
}
}
module.exports = { module.exports = {
truncate_decimal, truncate_decimal,
try_parse_json, try_parse_json,
copy_object copy_object,
run_regex
}; };