Created
November 23, 2015 13:14
-
-
Save Vannevelj/7431a14671df0aab8b23 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 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