diff --git a/README.md b/README.md index b611d7b..c67483b 100644 --- a/README.md +++ b/README.md @@ -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 +}); ``` diff --git a/index.js b/index.js index 9bc1124..598fc9a 100644 --- a/index.js +++ b/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 };