JavaScript function equivalent to PHP’s str_replace

Below is the JavaScript function worked like PHP’s str_replace.

[code lang=”js”]
<script type="text/javascript">
function str_replace (search, replace, subject)
var result = "";
var oldi = 0;
for (i = subject.indexOf (search); i > -1; i = subject.indexOf (search, i))
result += subject.substring (oldi, i);
result += replace;
i += search.length;
oldi = i;
return result + subject.substring (oldi, subject.length);


You can find more equivalent JavaScript function same as it works for PHP.


You can get the library of these functions by php.js or can directly download by below link.

Download php.default.min.js