Check out How to replace all dots in a string using JavaScript where a replaceAll method is created for strings. It seems to have great performance "Faster than using regex" and ive used it and it works quite well. You could also remove the toLowerCase within the function and increase performance if case isn't an issue.
From Fagner Brack on How to replace all dots in a string using JavaScript
/**
* ReplaceAll by Fagner Brack (MIT Licensed)
* Replaces all occurrences of a substring in a string
*/
String.prototype.replaceAll = function( token, newToken, ignoreCase ) {
var _token;
var str = this + "";
var i = -1;
if ( typeof token === "string" ) {
if ( ignoreCase ) {
_token = token.toLowerCase();
while( (
i = str.toLowerCase().indexOf(
token, i >= 0 ? i + newToken.length : 0
) ) !== -1
) {
str = str.substring( 0, i ) +
newToken +
str.substring( i + token.length );
}
} else {
return this.split( token ).join( newToken );
}
}
return str;
};
alert('okay.this.is.a.string'.replaceAll('.', ' '));
so for your code i would use something like this
$.get(path, function(data) {
var strData = data.toString();
strData.replaceAll("error", "<span class='error'>error</span>");
$(".mydiv").append($(strData));
})
Edit:
As suggested by Sergey Kochetov the .replace is significantly faster. So this would be a faster method.
$.get(path, function(data) {
var strData = data.toString();
strData.replace(/error/gi, "<span class='error'>error</span>");
$(".mydiv").append($(strData));
})