On line 22 (text = text.replace(smiley_array,smiley_img);) I'm not sure how to apply the global regular expression as explained towards the bottom of that page on Tizag. I've tried getting around the issue though no luck; thoughts please?
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>JavaScript Smiley Text Replacement</title>
</head>
<body>
<script type="text/javascript">
//<![CDATA[
var smiley_array = [':)',':D','8)'];
var smiley_xhtml = ['happy','big-grin','joe-cool'];
function smiley(text)
{
for (var i = 0; i< smiley_array.length; i++)
{
var word = smiley_array[i];
var smiley_img = '<img alt="Smiley" src="images/smilie_' + smiley_xhtml[i] + '.gif" />';
//word = new RegExp(word, 'gi');
text = text.replace(smiley_array[i],smiley_img);
}
return text;
}
document.write(smiley('Hello! :) I :) hope you enjoy :D your stay 8).'));
//]]>
</script>
</body>
</html>