Web Analytics Made Easy -
StatCounter replace() doesn't work & errors - CodingForum


No announcement yet.

replace() doesn't work & errors

  • Filter
  • Time
  • Show
Clear All
new posts

  • replace() doesn't work & errors

    Please help me figure out what the problem is with this code:

    var s1=document.f.b.value;
    var s1=s1.replace(/\/gi,"/");
    The second line is where the browser knit-picks a problem with an "Unterminated String Constant" error. I've tried
    var s1=s1.replace(/\//gi,"/");, with and without the "i" and "gi". The highlighted code ends the error popup, but it still doesn't work. (I suspect that it's trying to replace all instances of "\/", which there isn't any in a normal file path.)

    The code is supposed to change all the "\" to "/" and place the string into a hidden text box within the same form. The string originates from a FILE input.

    I've tried for hours to find a solution to the problem by searching the internet, but I can't seem to get a break on this one. Any constructive reply is welcome, and thanks.

  • #2
    var s1 = s1.replace(/\\/g, "/");

    The backslash must be escaped by another backslash.
    But your real problem is that the second var creates a new variable s1 which overwrites the previous one.

    “There are two kinds of failures: those who thought and never did, and those who did and never thought.” - Dr. Laurence J. Peter quotes (American "hierarchiologist", Educator and Writer, 1919-1990)

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.