new function for regex execution
This commit is contained in:
parent
9f1022ff18
commit
d849b45223
@ -17,4 +17,11 @@ const obj = {foo:'bar'};
|
||||
const copy = util.copy_object(obj);
|
||||
copy.foo = 'baz';
|
||||
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
|
||||
});
|
||||
```
|
||||
|
20
index.js
20
index.js
@ -45,8 +45,26 @@ function copy_object (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 = {
|
||||
truncate_decimal,
|
||||
try_parse_json,
|
||||
copy_object
|
||||
copy_object,
|
||||
run_regex
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user