\ keeps getting trimmed

Discussion in 'Programming' started by Site Owner, Aug 2, 2007.

  1. #1
    I have this code which works but when I put it in my page (uses Smarty) the \ before the 's' and after the 'n' get trimmed.
    
    <script type="text/javascript">
    
    var str="Hello my name is Andy Hello my name is John";
    document.write(str = str.replace(/\sHello/g, " n\Hello"));
    
    </script>
    Code (markup):
    Is there a substatute to \ that won't be stripped? I have tried the {literal}{/literal} thing, doesn't fix it.
     
    Site Owner, Aug 2, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    The backslash is a special character and probably should be escaped in your case. Like this:
    
    document.write(str = str.replace(/\\sHello/g, " n\\Hello"));
    
    Code (markup):
    And sure it isn't supposed to be \n instead of n\ ?
     
    nico_swd, Aug 2, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Try:
    {literal}
    <script type="text/javascript">
    
    var str="Hello my name is Andy Hello my name is John";
    document.write(str = str.replace(/\sHello/g, "\nHello"));
    
    </script>
    {/literal}
    Code (markup):
     
    krt, Aug 2, 2007 IP