String replace

J

jman

i'm trying to do a simple replace

document.write( "H<ello".replace("e","a") );

it just prints "H"

if i remove the < in the source string, then i get "H<allo" which is
what i expect.

why does it fail with < in the source string.

confused!
 
R

rf

jman said:
i'm trying to do a simple replace

document.write( "H<ello".replace("e","a") );

it just prints "H"

if i remove the < in the source string, then i get "H<allo" which is
what i expect.

why does it fail with < in the source string.

The browser thinks it's an H followed by an HTML opening tag for the invalid
ello element.

Use &lt;
 
L

Laser Lips

Try writting your own replace method and add this method to the String
class so that all strings can call this method.

<script type='text/javascript'>
String.prototype.replaceAll=function(from,to)
{
try
{
var str=this;
var idx = str.indexOf( from );
while ( idx > -1 )
{
str = str.replace( from, to );
idx = str.indexOf( from );
}
return str;
}catch(e){}
}
alert("H<ello".replaceAll("e","a"));
</script>
Graham Vincent
 
R

Richard Cornford

Try writting your own replace method and add this method to
the String class so that all strings can call this method.

<script type='text/javascript'>
String.prototype.replaceAll=function(from,to)
{
try
{
var str=this;
var idx = str.indexOf( from );
while ( idx > -1 )
{
str = str.replace( from, to );
idx = str.indexOf( from );
}
return str;
}catch(e){}
}
alert("H<ello".replaceAll("e","a"));
</script>

The existing - String.prototype.replace - already allows for replacing
all occurrences of a sub-string, by the use of an appropriate regular
expression argument, so this is probably redundant.

The OP's problem resulted from the use of - doucument.write - to write
a string with a left pointing chevron character, as that method writes
HTML and so its string arguments are interpreted as HTML source code,
where a left pointing chevron has meaning beyond the charter it
represents. The code above will exhibit the same behaviour if used in
the same context because the issue never had anything to do with the -
replace - method itself.

Richard.
 
R

rf

Laser said:
Try writting your own replace method and add this method to the String
class so that all strings can call this method.

<script type='text/javascript'>
String.prototype.replaceAll=function(from,to)
{
try
{
var str=this;
var idx = str.indexOf( from );
while ( idx > -1 )
{
str = str.replace( from, to );
idx = str.indexOf( from );
}
return str;
}catch(e){}
}
alert("H<ello".replaceAll("e","a"));
</script>
Graham Vincent

WTF are you talking about? The OP's problem is an HTML one.
 
S

SAM

Le 3/4/10 10:58 AM, Laser Lips a écrit :
Try writting your own replace method and add this method to the String
class so that all strings can call this method.

<script type='text/javascript'>
String.prototype.replaceAll=function(from,to)
{
try

With alert() you don't need all this tralala

alert("H<ello".replace("e","a")); // H<allo

(at least with Firefox)
 
S

SAM

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/,"&lt;a") );
document.write( "H<ello".replace("<","&lt;").replace('e','a') );
document.write( "H<ello".replace(/</,"&lt;").replace('e','a') );
document.write( "H<ello".replace(/</g,"&lt;").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
confused!

avoid to have '<' in your strings wrote in HTML by JavaScript

javascript:
var str = "H<ello";
str = str.replace(/</g,'&lt;');
document.write( str.replace("e","a") );
document.close(); // H<allo

Other example :

javascript:
var str = "H<ello the W<orld";
str = str.replace(/</g,'&lt;');
document.write( str.replace(/e/g,"a") );
document.close(); // H<allo tha W<orld
 
L

Laser Lips

The existing - String.prototype.replace - already allows for replacing
all occurrences of a sub-string, by the use of an appropriate regular
expression argument, so this is probably redundant.

Richard.

Whoops, sorry your right. Just jumping in there with out thinking.

Graham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top