473,382 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Need some help with replace()

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
Jun 17 '06 #1
3 2975
Roy W. Andersen wrote:
Obviously I'm in dire need of assistance, so I'll appreciate all the
help I can get :)


The functions on themself seem to work; tested with the following:

alert(stripSmileys(applySmileys(':)')));

This alerts ":)", as expected.

Check for errors, e.g. did you properly define the smileys array?
JW
Jun 17 '06 #2
Janwillem Borleffs wrote:
Check for errors, e.g. did you properly define the smileys array?


OTOH, you might also be faced with multiple occurrences of a specific smiley
and the current behaviour of the functions which only replace the first one.

To fix this, you can change the pattern passed to the replace() function to
include a "g" modifier. However, since the smileys contain reserved regexp
characters, which you then would have to escape, it might be simpler to do
something as follows:

function applySmileys(str) {
for (key in smileys) {
while (str.indexOf(key) > -1) {
str = str.replace(key, '...');
}
}
return str;
}
JW
Jun 17 '06 #3
Janwillem Borleffs wrote:
function applySmileys(str) {
for (key in smileys) {
while (str.indexOf(key) > -1) {
str = str.replace(key, '...');
}
}
return str;
}


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
Jun 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Danny | last post by:
I need an asp command to strip out from a string all extra punctuation such as apostrophe, comma, period, spaces dashes, etc etc and just leave the letters. Can anybody give me some ideas? ...
7
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
2
by: Barnes | last post by:
Hi, Can anyone please tell me how I can use the replace method to replace a character if it occures in more than one textbox without having to write separate function for each textbox. The...
4
by: Mel | last post by:
I have: <head><title>JUNK</title></head><body >HELLO</body> i need to strip off all tags <> and end up with the content "HELLO". can someone show me how ? thanks a whole bunch
43
by: SLH | last post by:
hi people. im trying to validate input received via a text area on an ASP page before writing it to a database. i cant use client side javascript due to policy, so it all has to happen on the...
9
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but...
7
by: ojsimon | last post by:
Hi I found this script on a forum and have been trying to make it work, but all it returns is a blank screen, i tried using the debug error reporting but got nothing from that either just a blank...
4
by: sandvet03 | last post by:
I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to...
3
by: jats | last post by:
I am a .NET programmer and i had recently got a web application designed in Classic ASP, to make some modifications. When i run the application and try to create a new user the following error...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.