Here you can find the source of format(options)
//Problem 1. Format with placeholders ///* w w w .j av a2 s . com*/ // Write a function that formats a string. The function should have placeholders, as shown in the example //Add the function to the String.prototype //Example: // // input output //var options = {name: 'John'}; //'Hello, there! Are you #{name}?'.format(options); ' Hello, there! Are you John' //var options = {name: 'John', age: 13}; //'My name is #{name} and I am #{age}-years-old').format(options); 'My name is John and I am 13-years-old' 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' }; var options02 = { name: 'John', age: 13 }; var string01 = 'Hello, there! Are you #{name}?'; var string02 = 'My name is #{name} and I am #{age}-years-old'; console.log(string01.format(options01)); console.log(string02.format(options02));
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; ...
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}));
"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){ ...
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; ...
String.prototype.format = function(opts) { var source = this, data = Array.prototype.slice.call(arguments, 0), toString = Object.prototype.toString; if (data.length) { data = data.length == 1 ? (opts !== null && (/\[object Array\]|\[object Object\]/.test(toString.call(opts))) ? opts : data) : data; return source.replace(/%\{(.+?)\}/g, function(match, key) { var replacer = data[key]; ...
String.prototype.format = function(param) { var formatted = this; for(i in param ){ var regexp = new RegExp('\\{'+i+'\\}', 'gi'); formatted = formatted.replace(regexp, param[i]); return formatted; };
var FORMAT_PARAMETER_PATTERN; FORMAT_PARAMETER_PATTERN = /\@\(([a-zA-Z](?:[a-zA-Z0-9_]*))\)/g; String.prototype.format = function(parameters) { var value; value = this.valueOf(); return value.replace(FORMAT_PARAMETER_PATTERN, function(match, name) { var _ref; return (_ref = parameters != null ? parameters[name] : void 0) != null ? _ref : "<>"; }); ...