Nodejs String Format format(options)

Here you can find the source of format(options)

Method Source Code

String.prototype.format = function(options) {
   var option,/* ww w  .j  a v  a2s  .c om*/
      regex,
      formatted = this;
   for (option in options) {
      regex = new RegExp('#{' + option + '}', 'g');
      formatted = formatted.replace(regex, options[option]);
   }

   return formatted;
};


console.log('Hello, there! Are you #{name}?'.format({
   name: 'John'
}));
console.log('My name is #{name} and I am #{age}-years-old'.format({
   name: 'John',
   age: 13
}));

Related

  1. format(obj)
    String.prototype.format = function(obj){
        var placeHolders = this.match(/#{\w+}/g),
            prop,
            result = this;
        for (var i = 0; i < placeHolders.length; i++) {
            prop = placeHolders[i].substring(2, placeHolders[i].length - 1);
            if (obj[prop]){
                result = result.replace(placeHolders[i], obj[prop]);
        return result;
    };
    var options = {name: 'John'};
    console.log('Hello, there! Are you #{name}?'.format(options));
    var options2 = {name: 'John', age: 13};
    console.log('My name is #{name} and I am #{age}-years-old'.format(options2));
    
  2. format(obj)
    String.prototype.format = function(obj) {
      var args = arguments;
      var str = this;
      return str.replace(/\{[\w\d_-]+\}/g, function(part) {
        part = part.slice(1, -1);
        var index = parseInt(part, 10);
        if (isNaN(index)) {
          return obj[part];
        } else {
    ...
    
  3. format(object)
    String.prototype.format = function (object) {
        var self=this;
        for (var item in object) {
            var replaceItem = object[item],
                regExp = new RegExp('#{' + item + '}');
            self=self.replace(regExp, replaceItem);
        return self;
    var object = {
        name: 'John',
        age: 15,
        gender: 'male'
    };
    var str = 'My name is #{name}, I am #{age}-years-old and my gender is #{gender}'.format(object);
    console.log('TASK 1:\n'+str);
    
  4. format(options)
    function solve(args) {
    String.prototype.format = function(options) {
        var prop,
            result = this;
        for (prop in options) {
            result = result.replace(new RegExp('#{' + prop + '}', 'g'), options[prop]);
        return result;
    };
    ...
    
  5. format(options)
    String.prototype.format = function(options) {
      var option,
        toReplace,
        str = this;
      for (option in options) {
        toReplace = new RegExp('#{' + option + '}');
        str = str.replace(toReplace, options[option]);
      console.log(str);
    ...
    
  6. format(options)
    String.prototype.format = function (options) {
      return this.replace(/(?:#{(\w+)})/g, function ($0, $1) {
        return options[$1];
      });
    };
    var options1 = {name: 'John'};
    console.log('Hello, there! Are you #{name}?'.format(options1));
    var options2 = {name: 'John', age: 13};
    console.log('My name is #{name} and I am #{age}-years-old'.format(options2));
    ...
    
  7. format(options)
    String.prototype.format = function(options) {
        var formattedStr = this
            ,option;
        for (option in options ) {
            var placeHolder = new RegExp('#{' + option + '}');
            formattedStr = formattedStr.replace(placeHolder, options[option]);
        return formattedStr;
    };
    ...
    
  8. format(options)
    String.prototype.format=function(options) {
        var string = this;
        for (var i in options) {
            var pattern = '#{' + (i) + '\\}';
            var regex = new RegExp(pattern, 'g');
            string = string.replace(regex, options[i]);
        return string;
    var options = {name: 'John'};
    console.log('Hello, there! Are you #{name}?'.format(options));
    var options = {name: 'John', age: 13};
    console.log('My name is #{name} and I am #{age}-years-old'.format(options));
    
  9. format(options)
    var jsConsole;
    String.prototype.format = function (options) {
        return this.replace(/(?:#{(\w+)})/g, function ($0, $1) {
            return options[$1];
        });
    };
    var exampleOne = { name: 'John' };
    jsConsole.writeLine('Hello, there! Are you #{name}?'.format(exampleOne));
    var exampleTwo = { name: 'John', age: 13 };
    ...