...

View Full Version : How do I replace with a string that has í ?



techron
08-21-2010, 03:16 PM
I have this string:

var str = "VERSE 3: Iíve been asleep for a while now";

I want to make it:

var strDesired = "I've been asleep for a while now";

so I tried

str = str.replace(new RegExp( str, "gi" ), strDesired);

The problem is with identifying the í as opposed to '.
Does anyone have a solution?

jmrker
08-21-2010, 04:42 PM
Have you tried this?


var str = "VERSE 3: I’ve been asleep for a while now";
str = str.replace(/’/g,"'");
alert(str);

techron
08-21-2010, 09:09 PM
That works with a plain string, but not with document.innerHTML.
Example

var targ = document.getElementById("cont");
targ.innerHTML = targ.innerHTML.replace(/’/g,"'");

The annoying quote remains.

Philip M
08-21-2010, 09:54 PM
<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.

techron
08-22-2010, 01:49 AM
I was not aware at the outset that the problem is the mysterious innerHTML.
This still does not work:

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/c/colbie_caillat/bubbly_ver7_crd.htm

Arty Effem
08-22-2010, 03:01 AM
I was not aware at the outset that the problem is the mysterious innerHTML.
This still does not work:

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:

<!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>

Old Pedant
08-22-2010, 06:12 AM
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.


<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.

techron
08-22-2010, 09:14 AM
It depends on how that character was generated in your markup. http://www.eki.ee/letter/chardata.cgi?ucode=2019

This works and validates:

<!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

Dormilich
08-22-2010, 05:31 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum