Skip to content

Instantly share code, notes, and snippets.

@Vannevelj
Created November 23, 2015 13:14
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Vannevelj/7431a14671df0aab8b23 to your computer and use it in GitHub Desktop.
var regex = /\[([^\.]*?)\.?([^\.\]]*)\]/g;
var input = "123 [VALUE] abc [234.VALUE] def [123.NONONO] xyz";
function getReplacement(firstGroup, secondGroup) {
if (firstGroup && secondGroup == 'VALUE') {
return new Promise(resolve => {
resolve('XXX');
});
}
if (!firstGroup && secondGroup == 'VALUE') {
return new Promise(resolve => {
resolve('YYY');
});
}
return new Promise(resolve => {
resolve('undefined');
});
}
var captures = input.match(regex); // with global flag
async.each(captures, function (item, done) {
const matchPerCapture = item.match(/\[([^\.]*?)\.?([^\.\]]*)\]/); // Without global flag
const id = matchPerCapture[1];
const attribute = matchPerCapture[2];
getReplacement(id, attribute)
.then(res => {
input = input.replace(item, res);
done();
});
}, function () {
console.log('finished replacing');
console.log('result: ' + input);
});
console.log('end of file');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment