var strReplaceAll = strText; var intIndexOfMatch = strReplaceAll.indexOf( "th" ); // Loop over the string value replacing out each matching // substring. while (intIndexOfMatch != -1){ // Relace out the current instance. strReplaceAll = strReplaceAll.replace( "th", "[X]" ) // Get the index of any next matching substring. intIndexOfMatch = strReplaceAll.indexOf( "th" ); } alert( strReplaceAll );