Skip to content

Instantly share code, notes, and snippets.

@grimmdev
Created September 1, 2015 02:55
Show Gist options
  • Save grimmdev/fd65e8608cfbd6ff377f to your computer and use it in GitHub Desktop.
Save grimmdev/fd65e8608cfbd6ff377f to your computer and use it in GitHub Desktop.
DroidScript/Javascript Functions
String.format = function() {
// The string containing the format items (e.g. "{0}")
// will and always has to be the first argument.
var theString = arguments[0];
// start with the second argument (i = 1)
for (var i = 1; i < arguments.length; i++) {
// "gm" = RegEx options for Global search (more than one instance)
// and for Multiline search
var regEx = new RegExp("\\{" + (i - 1) + "\\}", "gm");
theString = theString.replace(regEx, arguments[i]);
}
return theString;
}
// Example
var test = String.format('<a href="{0}/{1}/{2}" title="{2}">{2}</a>',
var1, var2, var3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment