Help to understand and recode javascript function to deal with special characters.

Posted by Cesar Lopez on Stack Overflow See other posts from Stack Overflow or by Cesar Lopez
Published on 2010-02-17T16:22:35Z Indexed on 2010/03/19 1:51 UTC
Read the original article Hit count: 304

Filed under:
|
|

Hi all,

I am trying to rewrite a javascript function since I was told this function its a bit nasty peace of code and it could be nicely written by a very kind user from here.

I have been trying to understand what the function does, therefore I could rewrite it properly, but since I dont fully understand how it works its a very difficult task.

Therefore I am looking for help and directions (NOT THE SOLUTION AS I WANT TO LEARN MYSELF) to understand and rewrite this function in a nicer way.

The function its been made for dealing with special characters, and I know that it loops through the string sent to it, search for special characters, and add what it needs to the string to make it a valid string.

I have been trying to use value.replace(/"/gi,"/""), but surely I am doing it wrong as it crashes.

Could anybody tell me where to start to recode function?

Any help would be appreciated.

My comments on the function are in capital letters. Code

<script type="text/javascript">
    function convertString(value){ 
     for(var z=0; z <= value.length -1; z++)
      {
        //if current character is a backslash||WHY IS IT CHECKING FOR \\,\\r\\n,and \\n?
        if(value.substring(z, z + 1)=="\\" && (value.substring(z, z + 4)!="\\r\\n" && value.substring(z, z + 2)!="\\n"))
          {//WHY IS IT ADDING \\\\ TO THE STRING?
             value = value.substring(0, z) + "\\\\" + value.substring(z + 1, value.length);
             z++;
           }   
        if(value.substring(z, z + 1)=="\\" && value.substring(z, z + 4)=="\\r\\n")
            {//WHY IS IT ADDING 4 TO Z IN THIS CASE?
             z = z+4;
            }      
        if(value.substring(z, z + 1)=="\\" && value.substring(z, z + 2)=="\\n")
            {//WHY IS IT ADDING 2 TO Z IN THIS CASE?
             z = z+2;
            }
        }
    //replace " with \"
    //loop through each character
     for(var x = 0; x <= value.length -1; x++){
      //if current character is a quote
      if(value.substring(x, x + 1)=="\""){//THIS IS TO FIND \, BUT HAVENT THIS BEEN DONE BEFFORE?
         //concatenate: value up to the quote + \" + value AFTER the quote||WHY IS IT ADDING \\ BEFORE \"?
         value = value.substring(0, x) + "\\\"" + value.substring(x + 1, value.length);
         //account for extra character
         x++;
         }
      }
   //return the modified string
   return(value);
  }
<script>

Comments within the code on capital letters are my questions about the function as I mention above.

I would appreciate any help, orientation, advise, BUT NOT THE SOLUTION PLEASE AS I DO WANT TO LEARN.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about recommendation