Nodejs String Format format(options)

Here you can find the source of format(options)

Method Source Code

//Problem 1//from   w  w  w .java2s  . c o m

String.prototype.format = function(options) {
   var prop,
   regex,
   result = this;
   for (prop in options) {
      regex = RegExp('#\{' + prop + '\}', 'gi');
      result = result.replace(regex, options[prop]);
   }
   return result;
}

var text = 'Hello, there! Are you #{name}? My name is #{name} and I am #{age}-years-old',
   options = {
      name: 'John',
      age: 13
   }
console.log('');
console.log('Problem 1 result:');
outputMessage = 'Input text: ' + text + '; Output text: ' + text.format(options);
document.getElementById('result1').innerHTML += '<br />' + outputMessage;
console.log(outputMessage);
document.getElementById('result1').innerHTML += '<br /><span class="explanation">The result is reported in the console also.</span>';
document.getElementById('result1').innerHTML += '<br /><span class="explanation">?he demo uses the options object from the second example.</span>';

Related

  1. format(options)
    String.prototype.format = function(options) {
        var option,
            regex,
            formatted = this;
        for (option in options) {
            regex = new RegExp('#{' + option + '}', 'g');
            formatted = formatted.replace(regex, options[option]);
        return formatted;
    ...
    
  2. format(options)
    String.prototype.format = function (options) {
        var option,
            regex,
            formatted = this;
        for (option in options) {
            regex = new RegExp('#{' + option + '}', 'g');
            formatted = formatted.replace(regex, options[option]);
        return formatted;
    ...
    
  3. 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));
    
  4. format(options)
    var phraseOne = 'Hello, there! Are you #{name}?';
    var phraseTwo = 'My name is #{name} and I am #{age}-years-old';
    String.prototype.format = function (options) {
        var option,
            fullString = this;
        for (option in options) {
            var placeholder = /#\{\w+}/;
            fullString = fullString.replace(placeholder, options[option]);
        return fullString;
    };
    console.log(phraseOne.format({name: 'John'}));
    console.log(phraseTwo.format({name: 'John', age: 13}));
    
  5. format(options)
    var options = {name: 'John'};
    String.prototype.format = function (options) {
        var result = this;
        function getNameOfPlaceholder(startIndex, text) {
            var name = '';
            for (var i = startIndex + 1; text[i] != '}'; i++) {
                name += text[i];
            return name;
    ...
    
  6. format(options)
    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}));
    
  7. format(options)
    "use strict";
    var formatPlace = function (options, str) {
        function replacer(match, i) {
            i = i.substring(2, i.length - 1);
            return options[i];
        return str.replace(/(#{[^{}]+})/gm, replacer);
    };
    String.prototype.format = function(options){
    ...
    
  8. format(options)
    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 options01 = { name: 'John' };
    ...
    
  9. format(opts)
    String.prototype.format = function(opts) {
        var option,
            regex,
            formatted = this;
        for (option in opts) {
            regex = new RegExp('#{' + option + '}', 'g');
            formatted = formatted.replace(regex, opts[option]);
        return formatted;
    ...