In JavaScript, how do I replace multiple values in a string with replace() function without using a regular expression? : <p>At work we're using a custom JavaScript engine that has regular expressions disabled for performance reasons. </p>

<p>I've gotten stuck with this problem for the last couple of hours. I simplified it for this post. Let's say I have this string:</p>

<pre><code>var str = "nice weather, nice car, nice clothes, nice tv";
</code></pre>

<p>I want to replace all occurrences of "nice" with "ugly".</p>

<p>JavaScript's <code>replace()</code> function does just that - it replaces "nice" with "ugly" but here's the problem - if I do <code>str.replace("nice", "ugly")</code> then JavaScript will replace only first occurance of "nice" with "ugly".</p>

<p>I know that the solution to replace all occurrences is to use a regular expression such as <code>/nice/g</code> to match globally and run it as <code>str.replace(/nice/g, "ugly")</code> but in my case, I can't use a regular expression (because as I mentioned we use a custom JavaScript engine with regular expression engine disabled.)</p>

<p>Bottom line is:
I can't figure out how to replace a string with just <code>replace()</code> function. How can I do it?</p>
