Sebastian P.R. Gingter
Sebastian P.R. Gingter

Reputation: 6085

JavaScript string.format function does not work in IE

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('')
}

Upvotes: 3

Views: 2566

Answers (2)

bobince
bobince

Reputation: 536615

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;

Upvotes: 1

YOU
YOU

Reputation: 123881

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

Upvotes: 3

Related Questions