String replace

Discussion in 'Javascript' started by jman, Mar 4, 2010.

  1. jman

    jman Guest

    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!
     
    jman, Mar 4, 2010
    #1
    1. Advertising

  2. jman

    rf Guest

    "jman" <> wrote in message
    news:...
    >
    > 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;
     
    rf, Mar 4, 2010
    #2
    1. Advertising

  3. jman

    Laser Lips Guest

    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
     
    Laser Lips, Mar 4, 2010
    #3
  4. 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.
     
    Richard Cornford, Mar 4, 2010
    #4
  5. jman

    rf Guest

    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.
     
    rf, Mar 4, 2010
    #5
  6. jman

    Jorge Guest

    On Mar 4, 4:29 am, "rf" <> wrote:
    > "jman" <> 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.
     
    Jorge, Mar 4, 2010
    #6
  7. jman

    SAM Guest

    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
     
    SAM, Mar 4, 2010
    #7
  8. jman

    SAM Guest

    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
     
    SAM, Mar 4, 2010
    #8
  9. jman

    Laser Lips Guest


    > 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
     
    Laser Lips, Mar 5, 2010
    #9
  10. jman

    Laser Lips Guest


    > 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.
     
    Laser Lips, Mar 5, 2010
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais
    Replies:
    1
    Views:
    387
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    352
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,535
    Masudur
    Feb 18, 2008
  4. Prasad S
    Replies:
    2
    Views:
    239
    Dr John Stockton
    Aug 27, 2004
  5. V S Rawat
    Replies:
    5
    Views:
    314
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page