0

How can I perform a case-sensitive string replacement in JavaScript? I am looking for something similar to PHP's str_replace.

var text = "this IS some sample text that is going to be replaced";
var new_text = replace_in_javascript("IS", "is not", text);
document.write(new_text);

should give

this is not some sample text that is going to be replaced

I would like to replace all the occurences.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
Vish
  • 4,508
  • 10
  • 42
  • 74
  • 1
    RegExp is what you want. Look at `replace()` and omit the `i` flag. Also, include the `g` flag for global matches. – jeremy Jan 24 '13 at 03:31
  • Seems like [this post addresses the issue](http://stackoverflow.com/questions/280793/case-insensitive-string-replacement-in-javascript). – Giacomo1968 Jan 24 '13 at 03:34
  • 1
    `str_ireplace` is case-insensitive, looks like you got it backwards in php – bfavaretto Jan 24 '13 at 03:41

2 Answers2

4

You can use a regular expression:

var str = 'is not';
str = str.replace(/IS/g, 'something'); // won't replace "is"

g is for global.

If you want case-insensitive results add the i flag, /is/gi.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • 1
    For this to really be an answer, you need to demonstrate the use of the `g` flag. Someone coming from PHP is obviously going to expect a global replace. You should also explain the `i` flag for the case-insensitive scenario so that the poster can use it both ways. – Nathan Wall Jan 24 '13 at 03:33
  • > I would like to replace all the occurences. – jeremy Jan 24 '13 at 03:34
  • 1
    I added the `g` flag, forgot bout that! @bfavaretto OP is asking case-sensitive. – elclanrs Jan 24 '13 at 03:35
  • @bfavaretto The example shows a sensitive replace, but his pre-explanation exhibits desire for the opposite. I think the OP was just confused. Sorry 'bout the "read the OP," I guess the OP was a bit ambiguous. – jeremy Jan 24 '13 at 03:39
  • @Nile It took me a while to realize the source of the confusion too. I just replaced my comments here with one for the OP up there. – bfavaretto Jan 24 '13 at 03:42
0

Works exactly same as PHP function:

function str_ireplace(search, replace, subject) {
  //  discuss at: http://phpjs.org/functions/str_ireplace/
  // original by: Martijn Wieringa
  //    input by: penutbutterjelly
  //    input by: Brett Zamir (http://brett-zamir.me)
  // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // improved by: Jack
  // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // bugfixed by: Onno Marsman
  // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // bugfixed by: Philipp Lenssen
  //   example 1: str_ireplace('l', 'l', 'HeLLo');
  //   returns 1: 'Hello'
  //   example 2: str_ireplace('$', 'foo', '$bar');
  //   returns 2: 'foobar'

  var i, k = '';
  var searchl = 0;
  var reg;

  var escapeRegex = function(s) {
    return s.replace(/([\\\^\$*+\[\]?{}.=!:(|)])/g, '\\$1');
  };

  search += '';
  searchl = search.length;
  if (Object.prototype.toString.call(replace) !== '[object Array]') {
    replace = [replace];
    if (Object.prototype.toString.call(search) === '[object Array]') {
      // If search is an array and replace is a string,
      // then this replacement string is used for every value of search
      while (searchl > replace.length) {
        replace[replace.length] = replace[0];
      }
    }
  }

  if (Object.prototype.toString.call(search) !== '[object Array]') {
    search = [search];
  }
  while (search.length > replace.length) {
    // If replace has fewer values than search,
    // then an empty string is used for the rest of replacement values
    replace[replace.length] = '';
  }

  if (Object.prototype.toString.call(subject) === '[object Array]') {
    // If subject is an array, then the search and replace is performed
    // with every entry of subject , and the return value is an array as well.
    for (k in subject) {
      if (subject.hasOwnProperty(k)) {
        subject[k] = str_ireplace(search, replace, subject[k]);
      }
    }
    return subject;
  }

  searchl = search.length;
  for (i = 0; i < searchl; i++) {
    reg = new RegExp(escapeRegex(search[i]), 'gi');
    subject = subject.replace(reg, replace[i]);
  }

  return subject;
}

Original: http://phpjs.org/functions/str_ireplace/

Daniel Omine
  • 141
  • 1
  • 4