//the string to work with:
var httpAddress = 'http://examplesite.com/folders/images/myphoto.jpg',
//the char(s) to look for:
charToRemp = '/',
//the char(s) to change to:
rempCharWith = '\\\\/';
//using RegExp
function regexp\_changeCharsTo(charIn, from, to) {
var regExpFrom = RegExp(from, 'g');
return charIn.replace(regExpFrom, to);
}
//using array map
function arrMap\_changeCharsTo(charIn, from, to) {
return charIn
.split('')
.map(function (chr) {
if (chr === from) {
return to;
}
return chr;
})
.join('');
}
//using for in loop
function forIn\_changeCharsTo(charIn, from, to) {
var fnString = '';
for (var cr in charIn) {
if (charIn\[cr\] === from) {
fnString += to;
} else {
fnString += charIn\[cr\];
}
}
return fnString;
}
//Test:
var regexp\_test = regexp\_changeCharsTo(httpAddress, charToRemp, rempCharWith);
var arrMap\_test = arrMap\_changeCharsTo(httpAddress, charToRemp, rempCharWith);
var forIn\_test = forIn\_changeCharsTo(httpAddress, charToRemp, rempCharWith);