JavaScript str_ireplace
Replaces all occurrences of search in haystack with replace / case-insensitive
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 | function str_ireplace ( search, replace, subject ) { // Replaces all occurrences of search in haystack with replace / case-insensitive // // version: 1008.1718 // discuss at: http://phpjs.org/functions/str_ireplace // + original by: Martijn Wieringa // + input by: penutbutterjelly // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + tweaked by: Jack // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Onno Marsman // + input by: Brett Zamir (http://brett-zamir.me) // + 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 (!(replace instanceof Array)) { replace = [replace]; if (search instanceof 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 (!(search instanceof 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 (subject instanceof 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; } |
Examples
» Example 1
Running
1 | str_ireplace('l', 'l', 'HeLLo'); |
Should return
1 | 'Hello' |
» Example 2
Running
1 | str_ireplace('$', 'foo', '$bar'); |
Should return
1 | 'foobar' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have str_ireplace goodness in JavaScript.
This doesn't seem to escape special regex characters, hence it doesn't work when replacing e.g. "$" with "foo", or am I missing something". The following function might help, though I still ran into problems with replacing ">"...
Misc.escapeRegex = function(s) {
return s.replace(/([\\\^\$*+[\]?{}.=!:(|)])/g, '\\$1');
}


Leandro Angelo
Jul 22nd