escaping or encoding line break

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    escaping or encoding line break

    The following line causes a "unterminat ed string literal" error when the string from a LONGTXT field contains a hard return (paragraph marker, whatever).

    [CODE=javascript]eval("var result = " + string)[/CODE]

    How can I escape and still preserve the hard return?
    Last edited by gits; May 3 '08, 09:42 AM. Reason: added code tags
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    Originally posted by Claus Mygind
    The following line causes a "unterminat ed string literal" error when the string from a LONGTXT field contains a hard return (paragraph marker, whatever).

    eval("var result = " + string)

    How can I escape and still preserve the hard return?


    I see that I need to use the replace( ) method

    ie:

    [CODE=javascript]newString = string.replace( ??, "\n")[/CODE]

    I just don't know how to identify the hard return in the string.
    Last edited by gits; May 3 '08, 09:45 AM. Reason: added code tags

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      try this:

      [CODE=javascript]var newString = string.replace(/(\r)/g, '\n')[/CODE]
      \r is a carriage return ...

      kind regards

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        Thanks for the reply. My problem is this. How do Identify the line break. It is not a visible character? I want to replace the line break with the escaped character. Or perhaps I need to identify both the "carriage return" and the "line break" and replace them with "\r\n"

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          Ok I think I may have figured it out

          //this line replace the escaped line feed with a carriage return
          var yData = xData.replace( /%A/g, "\r").

          But I am not sure if that is how identify the hidden carriage return

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            hmmm ... i think the following replace should work, when you want to replace the linebreak with a carriage return:

            [CODE=javascript]var yData = xData.replace(/(\n|\r\n)/g, '\r');[/CODE]
            kind regards

            Comment

            Working...