JavaScript version of PHP's str_replace() function

I spend a lot of my working day in PHP so when I come to coding in javascript I often get frustrated at the difference in functionality between similar sounding function names in the two languages. I use PHP's str_replace() function fairly frequently and often need to use it in JavaScript work. JavaScript has a function, replace(), which is a string replacement tool but it only works on the first instance it finds - this is functionally PHP lacks. I wanted a function that found and replaced all instances of the search term.

The solution

My solution works by splitting the string, on the search variable, into an array and then joining the array values back into a string on the replace variable. Essentially it looks like this:

string.split(search).join(replace);

There are many arguments about extending the DOM prototype, which I am not going to go into here, so I've provided two varieties of the same solution - just use which ever one you agree with!

Extending the DOM prototype

This code should be put near the beginning of your javascript files as it needs to be loaded before the function can be called. The first argument is the string to be searched for and the second argument is the string to replace it with.

String.prototype.str_replace = function(search, replace) {

    return this.split(search).join(replace);

}

Example usage

This example will only work if used after the prototyping code above.

var string = 'this-is-a-test-string';

string = string.str_replace('-', '_');

console.log(string); // Should output 'this_is_a_test_string'

Normal functions instead of extending

The method above uses the prototype object to add extra functionality to the String object. If you prefer to just use basic functions then it could be written like the example below. Please note: Because the string variable needs to be passed as an argument to the function the search and replace arguments are now second and third - instead of first and second - respectively.

function str_replace(string, search, replace) {

    return string.split(search).join(replace);

}

Example usage

This example should work with the str_replace() function placed anywhere in an accessible namespace.

var string = 'this-is-a-test-string';

string = str_replace(string, '-', '_');

console.log(string); // Should output 'this_is_a_test_string'

Case sensitivity

The function IS case sensitive, which is fine for the example above which replaces hyphens with underscores, but you may need it to be case insensitive if you're replacing actual human readable text. In order to make is case insensitive you can pass a regular expression to search argument, as shown in the example below.

var string = 'this is a case sensitive string';

string = string.str_replace(/SENSITIVE/i, 'INsensitive');  // Extending
string = str_replace(string, /SENSITIVE/i, 'INsensitive');  // Non-extending

console.log(string); // Should output 'this is a case INsensitive string