Le 3/4/10 3:12 AM, jman a écrit :
i'm trying to do a simple replace
document.write( "H<ello".replace("e","a") );
it just prints "H"
No, it prints :
H<allo
where "<allo" is seen as a tag by the browser.
if i remove the < in the source string, then i get "H<allo" which is
what i expect.
what's working :
// H<allo
document.write( "H<ello".replace(/<e/,"<a") );
document.write( "H<ello".replace("<","<").replace('e','a') );
document.write( "H<ello".replace(/</,"<").replace('e','a') );
document.write( "H<ello".replace(/</g,"<").replace('e','a') );
// Hallo
document.write( "H<ello".replace(/(?:[<])e/,"a") );
why does it fail with < in the source string.
alert("H<ello".replace("e","a")); // H<allo
avoid to have '<' in your strings wrote in HTML by JavaScript
javascript:
var str = "H<ello";
str = str.replace(/</g,'<');
document.write( str.replace("e","a") );
document.close(); // H<allo
Other example :
javascript:
var str = "H<ello the W<orld";
str = str.replace(/</g,'<');
document.write( str.replace(/e/g,"a") );
document.close(); // H<allo tha W<orld