If you are comfortable with performing a feature test and using a function to substitute a non-capturing split, consider the following approach. This method checks for support when the script is loaded and assigns the appropriate function to nonCaptureSplit, ensuring that the test is only conducted once.
It's important to note that if your input contains characters other than alphabetic or numerical ones, such as punctuation marks, the pattern will need to be escaped.
Revised Solution
This solution now utilizes a completely manual split in cases where support for non-capture split is not available.
// Perform a case-insensitive, non-capture split
var nonCaptureSplit = (function() {
// Feature test for non-capturing split
if ('ab'.split(/(a)/).length == 3) {
return function(str, pattern) {
var re = new RegExp('(' + pattern + ')','i');
return str.split(re);
};
// If support is lacking, perform a manual split
} else {
return function(str, pattern) {
var result = [];
var ts = str.toLowerCase();
var tp = pattern.toLowerCase();
var first = true;
while (ts.indexOf(tp) != -1) {
var i = ts.indexOf(tp);
if (i === 0 && first) {
result.push('', pattern);
ts = ts.substring(tp.length);
str = str.substring(tp.length);
} else if (i === ts.length - tp.length) {
result.push(str.substr(0,i), pattern);
ts = '';
str = '';
} else {
result.push(str.substring(0,i), pattern);
ts = ts.substring(i + pattern.length);
str = str.substring(i + pattern.length);
}
first = false;
}
result.push(ts.length? str : '');
return result;
};
}
}());
Test Cases:
alert(nonCaptureSplit('wa', 'wa')); // ,wa,
alert(nonCaptureSplit('qwqwaba', 'wa')); // qwq,wa,ba
alert(nonCaptureSplit('qwqwaba', 'qw')); // ,qw,,qw,aba
alert(nonCaptureSplit('qwqwaba', 'ba')); // qwqwa,ba,
alert(nonCaptureSplit('baaqwqbawaba', 'ba')); // ,ba,aqwq,ba,wa,ba,
alert(nonCaptureSplit("L'architecture du système d'information devient", "ARCH"));
// L',arch,itecture du systÃme d'information devient
alert(nonCaptureSplit("ARCHIMAG", "ARCH")); // ,ARCH,IMAG
In browsers without support for non–capturing split, this method may be slightly inefficient for large strings with numerous matches. However, testing has shown consistent results in Safari and IE 6. Please conduct thorough testing and provide feedback if any issues arise.
Keep in mind that this solution is specific to cases similar to the original post and may not be suitable as a general solution.