Hi,
I need to do some replace-calls on certain strings in order to replace
smiley glyphs and other keywords with graphical icons on the client.
Unfortunately, my knowledge of regular expressions is somewhat limited
to say the least, so I'm struggling with making it work as I wand.
What I have is an associative array like this:
smileys[':)'] = 'smile.gif';
smileys[':('] = 'sad.gif';
....and so on. What I need is to replace this both ways under different
circumstances (e.g. to glyphs when editing the text, and back to graphic
icon when viewing the text).
These are the functions I (intend to) use:
function applySmileys(str) {
for (key in smileys) {
str = str.replace(key, '<img src="images/smileys/' + smileys[key] +
'" alt="' + smileys[key] + '" />');
}
return str;
}
function stripSmileys(str) {
for (key in smileys) {
smiley = '<img src="images/smileys/' + smileys[key] + '" alt="' +
smileys[key] + '" />';
str = str.replace(smiley, key);
}
return str;
}
The first one is for converting from glyph to graphic, the second one
the other way around. Neither of them work in their current state, but
I've tried any combination I can think of both with and without regular
expressions, but I can not make it work. So - instead of listing
everything I'v tried, I'll simply ask: does anyone know how I can make
this work the way I've described?
I believe the problem is related to the parentheses in the text-glyphs,
or maybe the slashes in the image path, but I've tried terminating these
in various ways only to get different error-messages.
Obviously I'm in dire need of assistance, so I'll appreciate all the
help I can get :)
Cheers,
Roy W. Andersen
--
ra at broadpark dot no / http://roy.skyggenesdal.org/
"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama