Case insensitive replace all

Try regex:

'This iS IIS'.replace(/is/ig, 'as');

Working Example: http://jsfiddle.net/9xAse/

e.g:
Using RegExp object:

var searchMask = "is";
var regEx = new RegExp(searchMask, "ig");
var replaceMask = "as";

var result = 'This iS IIS'.replace(regEx, replaceMask);

String.prototype.replaceAll = function(strReplace, strWith) {
    // See http://stackoverflow.com/a/3561711/556609
    var esc = strReplace.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    var reg = new RegExp(esc, 'ig');
    return this.replace(reg, strWith);
};

This implements exactly the example you provided.

'This iS IIS'.replaceAll('is', 'as');

Returns

'Thas as Ias'

Tags:

Javascript