Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replace Function for double quote(") to single quote (')

    Just want to replace a double quote(") to single quote ('), but it doesn't replace. The value of classnote (a short paragraph) is return from db.

    // Split a classnote into array by space
    var words=classnote.split(" ")

    // Reset classnote = Empty
    classnote = '';

    // Replace double quote(") to single quote (') through LOOP
    for(i=0;i<words.length;i++)
    {
    classnotenote = classnotenote + words[i].replace('"',"'") + ' ';
    }
    Last edited by owen_yuen; 03-25-2005 at 05:54 PM.

  • #2
    Regular Coder
    Join Date
    Jul 2003
    Location
    Ontario, Canada
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try using the escape character in your function. This will treat the ' as an actual quote.

    Code:
    // Split a classnote into array by space
    var words=classnote.split(" ")
    
    // Replace double quote(") to single quote (') through LOOP
    for(i=0;i<words.length;i++)
    {
    classnotenote = classnotenote + words[i].replace('"', '\'') + ' ';
    }
    IC

  • #3
    New Coder
    Join Date
    Oct 2002
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, it still doesn't replace double quote(") to single quote .... ????

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Location
    Ontario, Canada
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what is the output (or result) you're getting from that function?

    IC

  • #5
    New Coder
    Join Date
    Oct 2002
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Example:

    "classnote" value return from database

    Function : classnote = It"s is a test. It"s is a test.
    Output: classnote = It"s is a test. It"s is a test.


    But I am looking for : classnote = It's is a test. It's is a test.

  • #6
    New Coder
    Join Date
    Aug 2004
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    User Regular Expression with /g option to replace all double quotations.

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    var mystr='Hi "how are" you';
    mystr=mystr.replace(/\"/g,'\'');
    alert(mystr);
    //-->
    </SCRIPT>
    By the way, why r u splitting your classnote? You dont need to split it if you just want to replace quotations. Use classnote inplace of mystr in above code.

    Have fun

    Afru.
    Last edited by afru; 03-25-2005 at 06:14 PM.

  • #7
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    With the little bit of code you have provided, I see no need to be splitting the string at spaces... Besides, nowhere do I see where you join() the split array again... Below is all you should need to convert all instances of double quotes with single quotes...

    Code:
    <script type="text/javascript">
     <!--//
      function convertQuotes(string){
      return string.replace(/"/g,"'");
      }
    
    // EXAMPLE USAGE:
      document.write(convertQuotes('It"s is a test. It"s is a test.'));
     //-->
    </script>
    However, why were the single quotes ever converted to double quotes in the first place?? I can understand that the single quotes would conflict with the serverside delimiters but... The single quotes contained within the string sent to the database should have been converted to their ascii equivelent and therefore would not need converting again when returned...

    .....Willy

  • #8
    New Coder
    Join Date
    Oct 2002
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks

  • #9
    Regular Coder
    Join Date
    Jul 2003
    Location
    Ontario, Canada
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I agree with Willy's comment... why were the single quotes being converted to double quotes anyways? If you're using a database that uses SQL (which is nearly all of them as far as I know, especially for a web site)... you can use the slashes to "escape" the quotes.... example...

    'INSERT INTO TABLE (Field) VALUES ('This doesn\'t make sense');

    If you're using PHP, it's even easier, there's a function called addslashes that will automatically escape characters that might cause DB hickups.

    IC

  • #10
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by IdentityCrisis
    I agree with Willy's comment... why were the single quotes being converted to double quotes anyways? If you're using a database that uses SQL (which is nearly all of them as far as I know, especially for a web site)... you can use the slashes to "escape" the quotes.... example...

    'INSERT INTO TABLE (Field) VALUES ('This doesn\'t make sense');

    If you're using PHP, it's even easier, there's a function called addslashes that will automatically escape characters that might cause DB hickups.

    IC
    Using PHP, there are numerous methods to address this problem...

    • addSlashes()
    • htmlspecialchars()
    • htmlentities()


    But the bottom line... this needs to be done serverside NOT clientside...
    Else, you are going to run across a user with javascript disabled whom will be able to submit the single quotes anyway...

    Personally, I would use htmlentities so you do not need to stripSlashes when returned...

    .....Willy


  •  

    Posting Permissions

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