 <p><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace" rel="nofollow">https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace</a></p> <p>You might use a regular expression with the "g" (global match) flag.</p> <pre><code>var entities = {'&lt;': '&amp;lt;', '&gt;': '&amp;gt;'}; '&lt;inputtext&gt;&lt;anotherinputext&gt;'.replace( /[&lt;&gt;]/g, function (s) { return entities[s]; } ); </code></pre>