Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2002
    Location
    NY, NY
    Posts
    3,975

    Javascript escaping characters in variable

    2 questions
    Say i have a variable..
    var Variable = "ABC123";
    1. I need to parse out any " ' " [single quotes] out.
    2. I need to parse out any " & " [Ampersand] out.

    How can I do this in javascript?

    Thanks.

    A general example of how to replace A with B in a variable will suffice.

  2. #2
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082
    PHP Code:
    function stripQuotes(str)
    {
       return 
    str.replace('\'''').replace('\\&''');

    The first parameter of replace() is a regular expression, read up on these if you want to do anything tricky, and make sure you escape any RE metacharacters.

    Note: vB is being lame and stripping the leading backslash from the first regular expression. It should be backslash-'.

  3. #3
    Join Date
    Dec 2002
    Location
    NY, NY
    Posts
    3,975
    dont i have to run a loop?
    so if the string is
    var text = " a'a'a'a' ";
    so that it replaces every ' not just the first one?

  4. #4
    Join Date
    Dec 2002
    Location
    NY, NY
    Posts
    3,975
    Basically this is where i need it escaped:
    var url = "site.com/FILE.jsp?type="+trackType+"&song="+trackName+"&artist="+trackArtist;
    var XMLHTTP = WScript.CreateObject("Microsoft.XMLHTTP");
    XMLHTTP.open ("GET", url, false);
    XMLHTTP.send("");
    The variables
    trackType, trackName and so on cannot contain an "&" because that would make the file.jsp see it as a seperate variable.
    i think that an " ' " also causes problems.


    Thanks.

Posting Permissions

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