Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 607197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:20:05+00:00 2026-05-13T17:20:05+00:00

I am trying to rewrite a javascript function since I was told this function

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:20:05+00:00Added an answer on May 13, 2026 at 5:20 pm

    Ok, let’s step through this.

    //if current character is a
    backslash||WHY IS IT CHECKING FOR
    \,\r\n,and \n?

    \ is a special character, known as an escape character. \, \r and \n are all escape sequences. There are several other javascript escape sequences, but these are the ones you are dealing with. If you look up that last link you will see that \ is the escape sequence for adding a backslash. Since \ itself is an escape character, adding a \ to a string requires you to add two. It does this for all groups of two \ as long as the escape sequence is not for the newline (\n), nor \r\n, a windows newline. When your string is later used, \ will end up being a single \ in the output.

    //WHY IS IT ADDING 4 TO Z IN THIS
    CASE?

    The reason the script is adding 4 and 2 to z in the other two ifs is because it has determined an escape sequence of that length, and therefor doesn’t need to check other characters in the sequence. As an example, consider the string `AAABAAACAAA’

    If I wanted to use the same method, looping through character by character, and change all instances of A to D, then I might do this:

    for (i = 0; i < myString.length; i++) {
        if (myString.substring(i) == 'A') {
            myString = myString.substring(0, i) + 'D' + myString.substring(i+1, myString.length);
        }
    }
    

    Instead, if I knew all of my A’s were in groups of 3, like they are in my case I could do this

    for (i = 0; i < myString.length; i++) {
        if (myString.substring(i, i+3) == 'AAA') {
            myString = myString.substring(0, i) + 'DDD' + myString.substring(i+3,myString.length);
            i+= 3;
        }
    }
    

    Here, I am finding an occurrence of AAA. The first time I find AAA my i = 0. Since I found AAA when i == 0, and I am replacing them with DDD, I know i + 1 and i + 2 are not going to contain a letter A (because i just replaced them)… so I can skip ahead and start my processing three characters down on the next loop.

    //THIS IS TO FIND \, BUT HAVENT THIS
    BEEN DONE BEFFORE?

    No, here you are looking for \”, the escape sequence for a double quote.

    Try this to see the difference in output.

    var testString = "This is a \"string\" with \"escape sequences\".\nIt \"escapes\" backslashes like this \\ and double quotes like this \" but leaves new lines alone";
    alert(testString);
    alert(convertString(testString)); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I finally figure this out! here's the code (for some… May 14, 2026 at 3:46 pm
  • Editorial Team
    Editorial Team added an answer This would be much easier if you were to create… May 14, 2026 at 3:46 pm
  • Editorial Team
    Editorial Team added an answer Starting with XQuery 1.1, use switch: http://www.w3.org/TR/xquery-11/#id-switch switch ($animal) case… May 14, 2026 at 3:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.