-
-
Save punmechanic/a7c4dbbb8815886e7e76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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