Here you can find the source of 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 String.prototype.format = function (options) { var option,//w w w . jav a2s . 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, #{name}! How are you?'.format({ name: 'Pesho' })); console.log('My name is #{name} and I am #{age} years old'.format({ name: 'Gosho', age: 25 }));
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; ...
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 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));
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}));
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; ...
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; ...