Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pmarkert/async-regex-replace
node.js library for regex replacements using asynchronous callback functions
https://github.com/pmarkert/async-regex-replace
Last synced: about 1 month ago
JSON representation
node.js library for regex replacements using asynchronous callback functions
- Host: GitHub
- URL: https://github.com/pmarkert/async-regex-replace
- Owner: pmarkert
- License: mit
- Created: 2015-06-18T17:26:11.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-05T17:04:07.000Z (over 7 years ago)
- Last Synced: 2024-04-26T02:41:08.444Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 9.77 KB
- Stars: 7
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# async-regex-replace
node.js library for regex replacements using asynchronous callback functions"All I needed" was a simple function to do string.replace(/regex/, callback_function) so that I could find some matches in a
string and then call my own custom function to return the value to be replaced back into the string. Sounds pretty easy right?
It is, as long as your callback function is syncrhonous. :)What happens if you need to call an asychronous function to determine the replacement value(s)?
Perhaps you are using the matched value to lookup the replacement in a database.## Enter async-regex-replace
``` js
'use strict';
var async_regex_replace = require('async-regex-replace');async_regex_replace.replace(/regex/g, "String with regex to replace", function(callback, match) {
setTimeout(function() {
var replacement_value = match.split('').reverse().join('');
var err = null;
callback(err, replacement_value);
}, 1000);
}, function(err, final_result) {
if(err) { console.log("Error - " + err); }
else {
console.log(final_result);
//> String with xeger to replace
}
});
```NOTE: In this particular example, the gratuitous use of setTimeout is just to demonstrate the asynchronous functionality in the replacement callback.
### Captured groups
If your regex contains capture groups, they will be passed in as extra parameters after the match but before the callback.