Questo codice deve essere incluso per far funzionare gli altri due:
codice:
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
Questo esegue il punto 1:
codice:
function sostituisci(str, find, replace) {
var r = new RegExp(find.join("|"), "g"),
f = function(m) {
return replace[ find.indexOf(m) ];
}
return str.replace(r,f)
}
Questo esegue il punto 2:
codice:
function sostituisciTags(str) {
var r = /<(.+?)>/g,
f = function(m, o1) {
return '<' + o1.replace(/</g, "") + '>';
}
return str.replace(r,f)
}