Results 1 to 4 of 4

Thread: "Unterminated String Literal" Problem/Error-Solved

  1. #1
    Sencha User dhruvakrishnan's Avatar
    Join Date
    Jul 2008
    Location
    Bangalore
    Posts
    26
    Vote Rating
    0
      0  

    Default "Unterminated String Literal" Problem/Error-Solved

    I wasn't sure as to where I had to post this. So I apologise for this.
    JSON doesn't handle the "Carriage Return" and the "Line Feed" actions in a proper way.
    This results in a "Unterminated String Literal" error in Javascript. I was trying to figure out a regular expression that could handle "Carriage Return" and the "Line Feed" actions appropriately. Here is what I did to solve the problem
    Code Snippet
    //Start
    .
    .
    .
    var jsonval = document.getElementById('candidateInfoJSON').value;
    .
    .
    .
    var txt = jsonval.replace(/(\u000A)+|(\n)+|\u000A|(\u000D)+|\u000D|\u000A\u000D|\n\r/, ','); //My Earlier Regular Expression
    .
    .
    .
    .
    var jsondecodedval = Ext.util.JSON.decode(txt);
    .
    .
    //End

    Sample input
    **********************
    Java
    J2EE
    Testing
    **********************
    Expected Output
    ***********************
    Java,J2EE,Testing

    My code is meant to handle "Line feeds" or accept "\n" or the new line character. But since JSON couldn't decode the variable "txt", Javascript threw an error saying"unterminated String Literal.
    It was a problem with the Regular Expression.
    So I changed my expression to something like this.

    var txt = jsonval.replace(/(\u000A)+|(\n)+|\u000A|(\u000D)+|\u000D|\u000A\u000D|\n\r/g, ', '); //<--Regular Expression

    The "g" at the end of the expression turns on this Regular expression globally so that any occurrence of "Line Feed" and/or "Carriage return" is handled.

    Please let me know if there is anything I've missed out or if there is anything wrong with this. It worked for me though.

  2. #2
    Ext JS Premium Member saJoshua's Avatar
    Join Date
    Nov 2007
    Location
    Sydney
    Posts
    522
    Vote Rating
    5
      0  

    Default

    Code:
    var json = {
        myValue1 : 'value1',
        myValue2 : 'value2',
        myFunction1 : function(params){
            // do stuff
        },
        myFunction2 : function(params){
            // do stuff
        }
    }
    as you can see, JSON handles carriage returns perfectly. Javascript doesn't handle strings that break onto new lines like this:

    Code:
    var mystring = "asfsdf
    asdf
    asdf"
    You'll need to parse these strings through your regular expression to work with them in javascript.

  3. #3
    Sencha User dhruvakrishnan's Avatar
    Join Date
    Jul 2008
    Location
    Bangalore
    Posts
    26
    Vote Rating
    0
      0  

    Default

    The work around which I used worked.The regular expression was able to parse it. I had a problem with understanding which one if it JavaScript/JSON had a problem with handling "Line Feeds". Thanks for throwing light on it saJoshua.

  4. #4
    Sencha Premium User NoahK17's Avatar
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    538
    Vote Rating
    2
      0  

    Default

    Yeah "g" is how you do multiple search and replaces across a string, and as you stated it stands for "global". You can also use "i" for case-insensitive searches.

    It's all documented on w3s: http://www.w3schools.com/jsref/jsref_replace.asp
    Noah
    Front-End Developer
    Norfolk Southern

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •