Janwillem Borleffs wrote:[color=blue]
> function applySmileys(str) {
> for (key in smileys) {
> while (str.indexOf(key) > -1) {
> str = str.replace(key, '...');
> }
> }
> return str;
> }[/color]
I actually made it work with this method while trying out several
different possibilities, but I figured that was the "hack" way of doing
it, and that a proper regexp-matching would be the cleaner and more
proper way.
I'm aware of the /g (and /gi) switches for regexp matching, and that's
been something of the core of my problem - once I turn it into a regexp
in order to match globally, the regexp rules apply, and what will match
without a regexp definition will no longer match.
As for the array, it is defined from a serverside array, all I do is
"smileys = new Array();" followed by a "smileys['glyph'] = 'imagefile';"
statement for each of the entries in my serverside array. I'm unaware of
any other way of defining it, so I've assumed this is correct.
Another thing that I now realised was causing problems, is that
JavaScript apparently converts my img-tag rather than parsing it
literally. I have them all written as <img parms="values" /> but
JavaScript reads them as <img parms="values"> (e.g. it ignores the
closing part of the tag). Maybe this is why I've been having to much
trouble with the pattern matching, as I've always matched it against the
properly formatted tag (as per XHTML that is).
So, after some testing with this discovery, I've found that this
actually works:
function stripSmileys(str) {
for (key in smileys) {
smiley = new RegExp('<img src="images/smileys/' + smileys[key] + '"
alt="' + smileys[key] + '">', 'gi');
str = str.replace(smiley, key);
}
return str;
}
However, this does not work, and results in an "unterminated
parenthetical" error in the Firefox JS-console.
function applySmileys(str) {
for (key in smileys) {
search = new RegExp(key, 'gi');
str = str.replace(search, <img src="images/smileys/' + smileys[key]
+ '" alt="' + smileys[key] + '" />');
}
return str;
}
I assume this happens because of the parentheses in the smileys, because
if I remove those in the array, and only leave the ones in the form of
:keyword:, the above function works as well.
I suppose I'll use the while indexOf-method on that one after all. I'm
just a bit curious still as to how exeactly this problem can be solved
without using this kind of workaround.
In any event, thank you for the replies - if nothing else it atleast
made me dig a bit more into it and figure out a couple of things I'd
missed previously :)
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