<section>
	<h1>Command</h1>
	<pre class="source">printf('apple:{0}, banana:{1}, mango:{2}', 'red', 'yellow', 'green');</pre>
</section>

<section>
	<h1>Result</h1>
	<pre class="result"></pre>
</section>

<hr />

<p>
출처 : <a href="http://stackoverflow.com/q/4974238" target="_blank">http://stackoverflow.com/q/4974238</a>
</p>
.source, .result {
	border: 1px solid #ccc;
	padding: 15px;
}
View Compiled
/**
 * printf
 *
 * @param {string} str
 * @param {string} values
 */
function printf(str, ...values)
{
	for (let i = 0; i < values.length; i++)
	{
		let pattern = `\\{${i}\\}`;
		let replace = new RegExp(pattern, 'g');
		str = str.replace(replace, values[i]);
	}
	return str;
}


// action
var result = printf('apple:{0}, banana:{1}, mango:{2}', 'red', 'yellow', 'green');
document.querySelector('.result').innerHTML = result;

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.