I think your best solution might be to use JavaScript's native replace method for strings.
W3Schools has a nice low-down on it here. It should do exactly what you want, but may replace ALL the numbers you specify, so be sure to say something like var replacement = str.replace("2 ", "28 ");
EDIT: Pointed out a good flaw with this. Instead you could try:
EDIT2: Opps, had some flaws in the original code. Tested and works fine! :)
function replaceNumbers(x1, x2, str) {
var strMod = " " + str + " "
var x1Mod = " " + x1 + " "
var x2Mod = " " + x2 + " "
// Want to replace "farthest" first to ensure correct replacement.
if (str.indexOf(x1Mod) > str.indexOf(x2Mod)) {
strMod = strMod.replace(x1Mod, x2Mod)
strMod = strMod.replace(x2Mod, x1Mod)
} else {
strMod = strMod.replace(x2Mod, x1Mod)
strMod = strMod.replace(x1Mod, x2Mod)
}
return strMod.slice(1, strMod.length - 1)
}
var numbers = "2 4 13 14 28 33";
alert(replaceNumbers(2, 33, numbers))