3

I have a JavaScript from this source in a comment of a blog: frogsbrain

It's a string formatter, and it works fine in Firefox, Google Chrome, Opera and Safari. Only problem is in IE, where the script does no replacement at all. The output in both test cases in IE is only 'hello', nothing more.

Please help me to get this script working in IE also, because I'm not the Javascript guru and I just don't know where to start searching for the problem.

I'll post the script here for convenience. All credits go to Terence Honles for the script so far.

// usage:
// 'hello {0}'.format('world');
// ==> 'hello world'
// 'hello {name}, the answer is {answer}.'.format({answer:'42', name:'world'});
// ==> 'hello world, the answer is 42.'
String.prototype.format = function() {
    var pattern = /({?){([^}]+)}(}?)/g;
    var args = arguments;

    if (args.length == 1) {
        if (typeof args[0] == 'object' && args[0].constructor != String) {
            args = args[0];
        }
    }

    var split = this.split(pattern);
    var sub = new Array();

    var i = 0;
    for (;i < split.length; i+=4) {
        sub.push(split[i]);
        if (split.length > i+3) {
            if (split[i+1] == '{' && split[i+3] == '}')
                sub.push(split[i+1], split[i+2], split[i+3]);
            else {
                sub.push(split[i+1], args[split[i+2]], split[i+3]);
            }
        }
    }

    return sub.join('')
}
Sebastian P.R. Gingter
  • 5,955
  • 3
  • 31
  • 73
  • I like this answer (http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery/1038930#1038930), it's much more concise :) – Mottie Mar 02 '10 at 15:07

2 Answers2

3

I think the issue is with this.

var pattern = /({?){([^}]+)}(}?)/g;
var split = this.split(pattern);

Javascript's regex split function act different in IE than other browser.

Please take a look my other post in SO

Community
  • 1
  • 1
YOU
  • 120,166
  • 34
  • 186
  • 219
  • Thanks. That pointed me in the correct direction. Allthough the link in the comments to your post did not solve this problem, I found a more general solution here: http://blog.stevenlevithan.com/archives/cross-browser-split – Sebastian P.R. Gingter Jan 28 '10 at 09:36
1

var split = this.split(pattern);

string.split(regexp) is broken in many ways on IE (JScript) and is generally best avoided. In particular:

  • it does not include match groups in the output array
  • it omits empty strings

    alert('abbc'.split(/(b)/)) // a,c

It would seem simpler to use replace rather than split:

String.prototype.format= function(replacements) {
    return this.replace(String.prototype.format.pattern, function(all, name) {
        return name in replacements? replacements[name] : all;
    });
}
String.prototype.format.pattern= /{?{([^{}]+)}}?/g;
bobince
  • 528,062
  • 107
  • 651
  • 834