Skip to content

Instantly share code, notes, and snippets.

@sharmaabhinav
Created November 27, 2017 10:08
Show Gist options
  • Save sharmaabhinav/bec27739098e2dd7e3bbc6f523d138d0 to your computer and use it in GitHub Desktop.
Save sharmaabhinav/bec27739098e2dd7e3bbc6f523d138d0 to your computer and use it in GitHub Desktop.
var re = /\{\d\}/
var str = 'My name is {0} . My location is {1}. My country is {2}'
function formatter (str) {
var params = Array.prototype.splice.call(arguments, 1)
for(var i=0;i<params.length;i++){
str = str.replace(re, params[i])
}
return str
}
console.log(formatter(str, 'abhinav', 'sharma', 'india'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment