Skip to content

Instantly share code, notes, and snippets.

@punmechanic
Forked from Vannevelj/holyshit.js
Last active November 23, 2015 13:28
Show Gist options
  • Save punmechanic/a7c4dbbb8815886e7e76 to your computer and use it in GitHub Desktop.
Save punmechanic/a7c4dbbb8815886e7e76 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 Promise.resolve('XXX');
}
if (!firstGroup && secondGroup == 'VALUE') {
return Promise.resolve('YYY');
}
return Promise.resolve('undefined'); // are you sure you meant for this to be a string?
}
const promises = input.match(regex).map(function(capture) {
const matchPerCapture = item.match(/\[([^\.]*?)\.?([^\.\]]*)\]/); // Without global flag
const id = matchPerCapture[1];
const attribute = matchPerCapture[2];
return getReplacement(id, attribute).then(res => input.replace(item, res));
});
Promise.all(promises).then(() => {
console.log('finished replacing');
console.log('end of file');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment