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.
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\ ?
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):