Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A CodePen by Craig Patik (http://cdpn.io/mtHBd)
var insertAfterEveryFromRight = function _insertAfterEveryFromRight(str, insert, every) {
var result = '';
str.split('').reverse().forEach(function(s, idx) {
if (idx !== 0 && idx % every === 0) {
result += insert;
}
result += s;
});
return result.split('').reverse().join('');
};
/*
Examples:
insertAfterEveryFromRight("1234567", ",", 3); // "1,234,567"
insertAfterEveryFromRight("51713", "/", 2); // "5/17/13"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.