views:

282

answers:

2

Hello,

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('')
}
+3  A: 

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

S.Mark
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
A: 

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