3

I need to create a function like Douglas Crockford's String.supplant:

if (typeof String.prototype.supplant !== 'function') {
    String.prototype.supplant = function (o) {
        return this.replace(/{([^{}]*)}/g, function (a, b) {
            var r = o[b];
            return typeof r === 'string' ? r : a;
        });
    };
}

what it does is:

var html = '<div>{title}<h3>{time}</h3><p>{content}</p></div>';
var object = {title: "my title", time: "12h00m", content:"blablabla"}
supplanted = html.supplant(object);
//supplanted returns:
//<div>my title<h3>12h00m</h3><p>blablabla</p></div>

i need, for a project for my tags to be different: instead of {tagname}, i need it to be [ns:tagname]

does anyone here have enough knowledge of regular expressions to help me out?
thank you very much

André Alçada Padez
  • 10,987
  • 24
  • 67
  • 120

2 Answers2

6

The following works:

if (typeof String.prototype.supplant !== 'function') {
    String.prototype.supplant = function (o) {
        return this.replace(/\[ns:([^\[\]]*)\]/g, function (a, b) {
            var r = o[b];
            return typeof r === 'string' ? r : a;
        });
    };
}

Do note, that the brackets are escaped (e.g., [] becomes \[\]) since they have a special meaning in regular expressions.

Example:

var html = '<div>[ns:title]<h3>[ns:time]</h3><p>[ns:content]</p></div>';
var object = {title: "my title", time: "12h00m", content:"blablabla"}
supplanted = html.supplant(object);
// "<div>my title<h3>12h00m</h3><p>blablabla</p></div>"
jensgram
  • 31,109
  • 6
  • 81
  • 98
  • @André Alçada Padez You're welcome. BTW: I'm a male — name is Jens Gram. I'm not [Jen's grandmother](http://stackoverflow.com/questions/4243984/get-the-last-classname-of-an-array-with-jquery/4244022#4244022) :) – jensgram Jul 28 '11 at 11:12
  • 1
    @André Alçada Padez No, I mixed up the comments. I was refering to "... as *she* was 2 minutes ..." in your comment to pimvdb. – jensgram Jul 28 '11 at 12:21
  • I have a quick question, what are a,b in the inner function? Where do they come from? – zer0c00l Aug 07 '11 at 17:29
  • @zer0c00l `a` and `b` are just the parameter names that I used for the first and second arguments in the anonymous replace function. A complete list is available at [developer.mozilla.org](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter). – jensgram Aug 22 '11 at 07:05
2

The regexp would need to be changed.

if (typeof String.prototype.supplant !== 'function') {
    String.prototype.supplant = function (o) {
        return this.replace(/\[ns:([^:\[\]]*)]/g, function (a, b) {
            var r = o[b];
            return typeof r === 'string' ? r : a;
        });
    };
}

var html = '<div>[ns:title]<h3>[ns:time]</h3><p>[ns:content]</p></div>';
var object = {title: "my title", time: "12h00m", content:"blablabla"}
supplanted = html.supplant(object);
//supplanted returns:
//<div>my title<h3>12h00m</h3><p>blablabla</p></div>

New regexp: \[ns:([^:\[\]]*)].

First a [ (special character while it is the literal [ here, so needs to be escaped).

Then ns:.

After that any amount of any characters that does not contain a [, ] or ::

(     // start group
[^    // none of following
:\[\] // : [ ]
]*    // any amount
)     // end group

Lastly a ].

pimvdb
  • 151,816
  • 78
  • 307
  • 352