5

I am new to ExtJS. I came across following piece of code:

Ext.String.format('<a href="mailto:{0}">{1}</a>',value+"@abc.com",value);

Now this will create a mailto link. But my query is that how Ext.String.format works and what else can I use it for?

2 Answers 2

2

Ext.String.format:

Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.

You can look at the source of the function and see it uses the formatRe regex (/\{(\d+)\}/g):

format: function(format) {
        var args = Ext.Array.toArray(arguments, 1);
        return format.replace(formatRe, function(m, i) {
            return args[i];
        });
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.

The following Ext.String.format is modified to accept formatters functions (Ext.util.Format) e.g. alert(Ext.String.format("{0} {1:usMoney} {2:date('Y-m-d')}", 10, 20, new Date()));

Here is modified code Code:

Ext.String._formatRe = /{(\d+)(?:\:([\w.])(?:((.?)?))?)?}/g; Ext.String._argRe = /(['"])(.?)\1\s(?:,|$)/g

    Ext.String.format = function(format) {
        var args = Ext.Array.toArray(arguments, 1),
            fm = Ext.util.Format;

        return format.replace(Ext.String._formatRe, function(m, idx, fn, fmArgs) {
            var replaceValue = args[parseInt(idx, 10)],
                values,
                match;

            if (fn) {
                values = [replaceValue];

                while (match = Ext.String._argRe.exec(fmArgs)) {
                    values.push(match[2]);
                }

                return fm[fn].apply(fm, values);
            }

            return replaceValue;
        });
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.