How to replace all characters in a string?

I have a string that is passed by parameter and I have to replace all occurrences of it in another string, ex:

function r(text, oldChar, newChar)
{
    return text.replace(oldChar, newChar); // , "g")
}

The characters passed could be any character, including ^, |, $, [, ], (, )...

Is there a method to replace, for example, all ^ from the string I ^like^ potatoes with $?

Answers


function r(t, o, n) {
    return t.split(o).join(n);
}

Need Your Help

What selector to use

jquery jquery-selectors

I have a table and would like to get to the second a of td in jquery I did

jqGrid - Detecting backend HTTP response code on edit

javascript jquery jqgrid edit

I have a jqGrid that I have added editing functionality through a PHP script.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.