JSFiddle - React, Tailwind, and code Playground
HTML
Results:
<pre id="preResults"></pre>
JavaScript
var testFunctions = [];
testFunctions.push(function replaceViaSplitJoin(inSource, inToReplace, inReplaceWith) {
return inSource.split(inToReplace).join(inReplaceWith);
});
testFunctions.push(function replaceAllViaRegex(input, stringToReplace, stringToReplaceWith){
//http://stackoverflow.com/a/2116614/5665980
return input.replace(new RegExp(stringToReplace,"g"),stringToReplaceWith)
});
testFunctions.push(function replaceSubstring(inSource, inToReplace, inReplaceWith) {
//http://stackoverflow.com/a/11019805/5665980
var outString = inSource;
while (true) {
var idx = outString.indexOf(inToReplace);
if (idx == -1) {
break;
}
outString = outString.substring(0, idx) + inReplaceWith +
outString.substring(idx + inToReplace.length);
}
return outString;
});
String.prototype.replaceAllBySubstrings = function(_f, _r, _c){
//http://stackoverflow.com/a/23209168/5665980
var o = this.toString();
var r = '';
var s = o;
var b = 0;
var e = -1;
if(_c){ _f = _f.toLowerCase(); s = o.toLowerCase(); }
while((e=s.indexOf(_f)) > -1)
{
r += o.substring(b, b+e) + _r;
s = s.substring(e+_f.length, s.length);
b += e+_f.length;
}
// Add Leftover
if(s.length>0){ r+=o.substring(o.length-s.length, o.length); }
// Return New String
return r;
};
testFunctions.push(function replaceAllBySubstrings(inSource, inToReplace, inReplaceWith){
return inSource.replaceAllBySubstrings(inToReplace,inReplaceWith);
})
String.prototype.replaceAllSaferRegex = function(str1, str2, ignore)
{
//http://stackoverflow.com/a/6714233/5665980
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
}
testFunctions.push(function replaceAllSaferRegex(inSource, inToReplace, inReplaceWith){
return inSource.replaceAllSaferRegex(inToReplace,inReplaceWith);
})
testFunctions.push(function...