Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > String replace

Reply
Thread Tools

String replace

 
 
jman
Guest
Posts: n/a
 
      03-04-2010

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!

 
Reply With Quote
 
 
 
 
rf
Guest
Posts: n/a
 
      03-04-2010

"jman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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;


 
Reply With Quote
 
 
 
 
Laser Lips
Guest
Posts: n/a
 
      03-04-2010
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
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      03-04-2010
On Mar 4, 9:58 am, Laser Lips wrote:
> 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.
 
Reply With Quote
 
rf
Guest
Posts: n/a
 
      03-04-2010
Laser Lips wrote:
> 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.


 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      03-04-2010
On Mar 4, 4:29*am, "rf" <(E-Mail Removed)> wrote:
> "jman" <(E-Mail Removed)> wrote in message
>
> > document.write( "H<ello".replace("e","a") );

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


allo not ello.
--
Jorge.
 
Reply With Quote
 
SAM
Guest
Posts: n/a
 
      03-04-2010
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)

--
sm
 
Reply With Quote
 
SAM
Guest
Posts: n/a
 
      03-04-2010
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

--
sm
 
Reply With Quote
 
Laser Lips
Guest
Posts: n/a
 
      03-05-2010

> 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

 
Reply With Quote
 
Laser Lips
Guest
Posts: n/a
 
      03-05-2010

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


yeah I realized that later on, If you botherd to read my other post.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
how to make replace function replace globally in a string V S Rawat Javascript 5 07-03-2007 08:02 PM
Re: [Pyrex] pyrex functions to replace a method (Re: replace a method Greg Ewing Python 2 06-29-2006 05:25 PM
pyrex functions to replace a method (Re: replace a method in class:how?) Brian Blais Python 1 06-27-2006 12:13 PM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments