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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How do I replace with a string that has ’ ?

    I have this string:
    Code:
    var str = "VERSE 3: I’ve been asleep for a while now";
    I want to make it:
    Code:
    var strDesired = "I've been asleep for a while now";
    so I tried
    Code:
    str = str.replace(new RegExp( str, "gi" ), strDesired);
    The problem is with identifying the ’ as opposed to '.
    Does anyone have a solution?

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts
    Have you tried this?
    Code:
    var str = "VERSE 3: I’ve been asleep for a while now";
    str = str.replace(/’/g,"'");
    alert(str);

  • #3
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    works with string but not document.getElementById

    That works with a plain string, but not with document.innerHTML.
    Example
    Code:
    var targ = document.getElementById("cont");
    targ.innerHTML = targ.innerHTML.replace(/’/g,"'");
    The annoying quote remains.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Code:
    <span id = "cont">VERSE 3: I’ve been asleep for a while now</span><br>
    <span id = "cont1"></span>
    
    <script type = "text/javascript">
    var targ = document.getElementById("cont").innerHTML;
    targ = targ.replace(/’/g,"'");
    document.getElementById("cont1").innerHTML=targ;
    
    </script>
    Compare the two strings. But why did you not specify your problem properly at the outset?

    This wind won't make it easier, but it will make it less difficult. Golf commentator, BBC Five Live.

  • #5
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I was not aware at the outset that the problem is the mysterious innerHTML.
    This still does not work:
    Code:
    var targ = document.getElementById("cont");
    targ.innerHTML = targ.innerHTML.replace(/’/g,"'");
    Maybe encoding or decoding would work?

    I am using a Greasemonkey script to changed the html at http://www.ultimate-guitar.com/tabs/...y_ver7_crd.htm

  • #6
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by techron View Post
    I was not aware at the outset that the problem is the mysterious innerHTML.
    This still does not work:
    Code:
    var targ = document.getElementById("cont");
    targ.innerHTML = targ.innerHTML.replace(/’/g,"'");
    It depends on how that character was generated in your markup. http://www.eki.ee/letter/chardata.cgi?ucode=2019

    This works and validates:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <title>Test Page</title>
    <style type='text/css'>
    body{font-family:system;font-size:1.5em}
    </style>
    </head>
    <body>
    <p>
    <span id='cont'>VERSE 3: I&rsquo;ve been asleep for a while now</span><br>
    <input type='button' value='Replace' onclick='change()'>
    <script type='text/javascript'>
    function change()
    {
     var targ = document.getElementById("cont");
     targ.innerHTML = targ.innerHTML.replace(/\u2019/g,"'");
    }
    </script>
    </body>
    </html>

  • Users who have thanked Arty Effem for this post:

    techron (08-22-2010)

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    I didn't have any trouble using the ’ character in a regexp. I just copy/pasted it in place, rather than trying to type it in.
    Code:
    <html>
    <body>
    <div id="THIS">
    VERSE 3: I’ve been asleep for a while now
    You tucked me in just like a child now
    Cause every time you hold me in your arms
    I'm comfortable enough to feel your warmth
    </div>
    <a href="#" onclick="alert(document.getElementById('THIS').innerHTML); return false;">click</a>
    <a href="#" onclick="var d=document.getElementById('THIS');d.innerHTML=d.innerHTML.replace(/[’\']/g,'$'); return false;">clack</a>
    </body>
    </html>
    I put a normal apostrophe into "I'm" just to prove that you can replace either of them.

    p.s.: Tried with both IE and FF and both worked fine.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up yes, the character code worked

    Quote Originally Posted by Arty Effem View Post
    It depends on how that character was generated in your markup. http://www.eki.ee/letter/chardata.cgi?ucode=2019

    This works and validates:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <title>Test Page</title>
    <style type='text/css'>
    body{font-family:system;font-size:1.5em}
    </style>
    </head>
    <body>
    <p>
    <span id='cont'>VERSE 3: I&rsquo;ve been asleep for a while now</span><br>
    <input type='button' value='Replace' onclick='change()'>
    <script type='text/javascript'>
    function change()
    {
     var targ = document.getElementById("cont");
     targ.innerHTML = targ.innerHTML.replace(/\u2019/g,"'");
    }
    </script>
    </body>
    </html>
    Thanks man. I didn't find that character code in my standard ascii character table. The innerHTML must store that encoded escape sequence. I couldn't figure out how to search that site you referenced for a character, but I found another character search here: http://leftlogic.com/projects/entity-lookup

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by techron View Post
    I didn't find that character code in my standard ascii character table.
    that’s because the ASCII character table has only 128 places, while typographical characters are somewhere around place 8,200+ in the Unicode table.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

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