Il primo carattere di una stringa è 0 e non 1. Quindi search(pippo) >= 1 non prende in conto il primo carattere ma il secondo.
Prova cosi
codice:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/JavaScript">
function sostituisci_caratteri(valore) {
var stringa=valore;
//
if (stringa.search(/\</g)>=0 || stringa.search(/\>/g)>=0 || stringa.search(/\s/g)>=0 ) {
stringa=stringa.replace(/\s/g," ");
stringa=stringa.replace(/\</g,"<");
stringa=stringa.replace(/\>/g,">");
alert("I caratteri apice, doppio apice e space sono stati rimossi");
}
return(stringa);
}
alert(sostituisci_caratteri(" sono stato tranquillo per anni ."));
document.write(sostituisci_caratteri(" sono stato tranquillo per anni ."));
//-->
</script>
</head>
<body>
</body>
</html>