Javascript String format(options)

Description

Javascript String format(options)


String.prototype.format = String.prototype.f = function(options) {

    options = options ? options : [];// w  w  w  . j  a v a 2 s.c  o m
    if (typeof options != "object") options = [options];

    var s = this,
        i = options.length;

    while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), options[i]);
    }        

    return s;
 };

Javascript String format(options)

String.prototype.format = function (options) {
 return this.replace(/(?:#{(\w+)})/g, function ($0, $1) {
  return options[$1];
 });//from w ww .  j a va 2s  .c o m
};

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));

Javascript String format(options)

String.prototype.format = function (options) {
    var prop,//ww w  . j  av a2s. co m
        result = this;
    for (prop in options) {
        result = result.replace(new RegExp('#{' + prop + '}', 'g'), options[prop]);
    }
    return result;
};

var options = {
    name: 'John',
    age: 13
},
    result = 'My name is #{name} and I am #{age}-years-old.'.format(options);

document.writeln( result);

Javascript String format(options)

String.prototype.format = function(options) {
 var option,/*from w  w w .j ava 2  s . c  o  m*/
  toReplace,
  str = this;

 for (option in options) {
  toReplace = new RegExp('#{' + option + '}');
  str = str.replace(toReplace, options[option]);
 }

 console.log(str);
}

var options = {name: 'John'};
'Hello, there! Are you #{name}?'.format(options);

options = {name: 'John', age: 13};
'My name is #{name} and I am #{age}-years-old'.format(options);

Javascript String format(options)

//01.Write a function that formats a string. The function should have placeholders, as shown in the example
//Add the function to the String.prototype
'use strict';/*from w  ww  . j  a v a2s .  co  m*/
String.prototype.format = function (options){
    var result = this.replace(/#{([A-z]+)}/g, function($0, $1){
        return options[$1];
    });

    return result;
};
var text = 'My name is #{name} and I am #{age}-years-old',
    options = {name: 'John', age: 13},
    result;

result = text.format(options);

console.log(result);

Javascript String format(options)

'use strict';// www .ja v a  2s  .c o m
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;
};

var options = JSON.parse(args[0]);
    var result = args[1].format(options);

console.log(result);
}
solve(['{ "name": "John" }',
"Hello, there! Are you #{name}?"]);

Javascript String format(options)

String.prototype.format = function(options) {
 var option,/*from  w w w .  j a v a2s  .co m*/
  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
}));

Javascript String format(options)

String.prototype.format = function(options) {
    var formattedStr = this
        ,option;//from w w  w  .  ja  v a2s  . c  om
    for (option in options ) {
        var placeHolder = new RegExp('#{' + option + '}');
        formattedStr = formattedStr.replace(placeHolder, options[option]);
    }
    return formattedStr;
};

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}));

Javascript String format(options)

//Write a function that formats a string. The function should have placeholders, as shown in the example
//Add the function to the String.prototype.

String.prototype.format = function (options) {
    return this.replace(/(?:#{(\w+)})/g, function ($0, $1) {
        return options[$1];
    });//  w  ww  . jav  a2s .  c  o  m
};

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));

Javascript String format(options)

// Invoke replace callback function, that takes input string and first match as parameters
String.prototype.format = function (options) {
 return this.replace(/(?:#{(\w+)})/g, function ($0, $1) {
  return options[$1];
 });//from   w w w  .  j  ava  2 s  . c om
};

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));

Javascript String format(options)

// Problem 1. Format with placeholders

// Write a function that formats a string. The function should have placeholders.
// Add the function to the String.prototype

String.prototype.format = function(options) {
 return this.replace(/(?:#{(\w+)})/g, function($0, $1) {
  return options[$1];
 });/*from  w  w  w.  j  a va2s .co m*/
};

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));

Javascript String format(options)

console.log('Task 1: Write a function that formats a string. The function should have placeholders, as shown in the example. Add the function to the String.prototype')

String.prototype.format = function (options) {
    var option,/*from w  ww .j  a  v a  2  s .c o m*/
        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 }));

Javascript String format(options)

/**Write a function that formats a string. The function should have placeholders, as shown in the example
 Add the function to the String.prototype*/
console.log('TASK 1');

var text = 'My name is #{name}. I have #{color} eyes and I am #{age}-years-old';

String.prototype.format = function(options) {
    var option,//w  w  w  .  j  a v a 2 s . c  om
        result = this;
    for (option in options) {
        result = result.replace(new RegExp('#{' + option + '}', 'g'), options[option]);
    }
    return result;
};
var person = {
    name: 'John',
    age: 26,
    color: 'blue'

};

var print = text.format(person);
console.log(print);

Javascript String format(options)

String.prototype.format = function(options) {
    var result = String(this),
        regex;//from   ww  w.  ja  v  a 2  s .  com
    for(var prop in options) {
        regex = new RegExp('#{' + prop + '}', 'g');
        if(options.hasOwnProperty(prop)) {
            result = result.replace(regex, options[prop]);
        }
    }
    return result;
};

//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));

Javascript String format(options)

/**//from   w  w  w . java 2s.  co  m
 Problem 1. Format with placeholders
 Write a function that formats a string. The function should
 have placeholders, as shown in the example
 Add the function to the String.prototype
 */

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));

Javascript String format(options)

// 01. Write a function that formats a string. The function should have placeholders, as shown in the example
// Add the function to the String.prototype

var jsConsole;/*from   www  .  ja  va2s .c om*/

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 };
jsConsole.writeLine('My name is #{name} and I am #{age}-years-old'.format(exampleTwo));

Javascript String format(options)

/**//w ww .  j  av  a2 s  .  co m
 * Problem 1. Format with placeholders

 Write a function that formats a string. The function should have placeholders, as shown in the example
 Add the function to the String.prototype
 */
String.prototype.format = function(options) {
    var prop,
        result = this;
    for (prop in options) {
        result = result.replace(new RegExp('#{' + prop + '}', 'g'), options[prop]);
    }
    return result;
};

var options = {
        name: 'John',
        age: 13
    },
    result = 'My name is #{name} and I am #{age}-years-old.'.format(options);

console.log(result);

Javascript String format(options)

/* Problem 1. Format with placeholders

Write a function that formats a string. The function should have placeholders, as shown in the example
Add the function to the String.prototype
*//* w ww  . jav  a2  s .c  o m*/

String.prototype.format = function (options) {

    var regex,
        input = this;

    for (var key in options) {
        regex = new RegExp('#{' + key + '}', 'g');
        input = input.replace(regex, options[key]);
    }

    return input;
}

console.log('Original text is: Hello, there! Are you #{name}? and Object is: { name: \'John\' }')
console.log('Changed text is: ' + 'Hello, there! Are you #{name}?'.format({ name: 'John' }));

console.log('Original text is: My name is #{name} and I am #{age}-years-old).format(options) and object is: {name: \'Johnny\', age: 21} ');
console.log('Changed text is: ' + 'My name is #{name} and I am #{age}-years-old)'.format({name: 'Johnny', age: 21}));



PreviousNext

Related