Here you can find the source of 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){ var option,// w w w. ja v a2 s .c om result = this; for (option in options){ var regex = new RegExp('#{' + option + '}'); result = result.replace(regex,options[option]); } 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));
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 }; ...
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 = { ...
String.prototype.format = function(options) { var option, regex, result = this; for (option in options) { regex = new RegExp('#{' + option + '}', 'g'); result = result.replace(regex, options[option]); return result; ...
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; ...
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, result = this; for (option in options) { result = result.replace(new RegExp('#{' + option + '}', 'g'), options[option]); return result; ...
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; ...
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; ...
console.log('1. Format with placeholders'); 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; }; 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}));
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; ...