• I think that

    Making the Web is like playing a game. Standard, Accessibility and Usability are only few rules.
    No game is awesome if you don't follow the rules.

    International Webmasters Association

  • Subscribe to my blog via email

    Insert here your e-mail address and you will receive a message when a new post will come.

enhanced sprintf like function in Javascript

Today I’was playing with javascript and regular expression, and I made a PHP sprintf like function with some limitations and some enhancements.

The method will accept 2 parameters: the first is the string buffer that has the markers and the second parameter could be both an Object or an Array. In the first (the enhancement) case the method will replace all the occourrences of each key in the string with related value. In the second case its behavior will be the same (or at least similar) as sprintf.

As written before, there are some limitations:

  1. The method in the classic way will consider only %s and %d, %n$s and %n$d in the string buffer.
  2. Float numbers will be threated as %d.
  3. Hex values that contain at least a value between A and F will be threated as %s others as %d.
  4. Actually no string formatting is provided.
function sprintt(s, params){

        // We made a clone of the "s" string
    var newS = s,

       // and initializing a loop index
       currentIndex = 0;

    // Param will be a Json made list of placeholders.
    for(param in params){

        // Index will start by 1

            // If the params object is an indexed array

            var value = params[param];

            // Actually the method will support only numbers (d) 
            // and strings (s).
            // Float numbers will be threath as numbers.
            // Any support for octal/hex transformation.
            // Neither for string filler. 
                 param = 'd';
                 param = 's';
            // the regular expression must check only for the first 
            // occourrence of the given placeholder (as in sprintf)
            var regEx = new RegExp('%'+param);
            newS = newS.replace(regEx, value);

            // and then must replace all the %x$1 occourrences in the 
            // string, where "x" is the current index entry (as in sprintf)
            regEx = new RegExp('%'+currentIndex + '$'+param, 'g');
            // Else we have a json "key/value" pair object and we have 
            // to replace all the given tokens
            var regEx = new RegExp('%'+param, 'g');
            value = params[param];
        newS = newS.replace(regEx, value);

    return newS;

Here there are two ways to use it:

// Example #1: Standard way 
// Output of will be: "Visit every 10 days! Diego will thanks you!"
        'Visit %s every %d days! %s will thanks you!', 
// Example #2 - Enhanced mode
// Output of will be: "Visit every 10 days! Diego will thanks you!"

       'Visit %url every %numberOfDays days! %name will thanks you!',
           url: '',
           numberOfDays: 10,
           name: 'Diego'

Feel free to use it wherever you want, if you did some enhancement of the above code please, tell me, I will glad to post it here!


3 thoughts on “enhanced sprintf like function in Javascript

    • Hi Bojan,
      thanks for your reply, in this days I’m working to the new version of that method. Maybe I will post news about it in one of the next weeks. :)

      As you’ve written the “supplant” method is not the same: the method implemented by Douglas Crockford become a String’s object method via String.prototype mine instead is a standalone method and nearest to the PHP implementation of sprintf.