What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Replacing "?"

Replacing "?"
Author: Message:
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
32 / Male / Flag
Joined: Jul 2006
O.P. Replacing "?"
I am trying to write something that replaces ? with ..? but to no success.

i want it so that if there is a ? at then end of the message, replace it with a ..? but i cant seem to do it :p
Any help?
05-03-2007 04:43 PM
Profile E-Mail PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
RE: Replacing "?"
That's possible with a simple regex.

code:
stringObj.replace(/^(.+)(\?)$/i, \1..\2)

[Image: 1-0.png]
             
05-03-2007 05:01 PM
Profile PM Web Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
32 / Male / Flag
Joined: Jul 2006
O.P. RE: Replacing "?"
oh ok :$
Thanks
05-03-2007 06:01 PM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Replacing "?"
quote:
Originally posted by Ezra
code:
stringObj.replace(/^(.+)(\?)$/i, \1..\2)

You forgot the "" around the second parameter. The first is a regular expression object and is totally correct, but the second is a string. Also, the regular expression can be lots and lots simpler. Therefore, the right way would be:
code:
stringObj.replace(/\?$/, "..?");
Here, I don't let it capture everything before the question mark because it simply isn't needed. This gives you a much easier to understand regular expression without the need of inserting captured parameters! ;)

Because remember to KISS! (Keep It Short and Simple)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
05-03-2007 06:28 PM
Profile E-Mail PM Web Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
RE: Replacing "?"
Thanks for fixing up my mistake :-)
I thought the second parameter was a regex object too.

KISS is very true :P, but I always tend to make things harder then they should be :(
[Image: 1-0.png]
             
05-03-2007 07:02 PM
Profile PM Web Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Replacing "?"
? What?!?

will turn into:

..? What..?!..?

with that reg expression.... Maybe something to take into account that the question mark must be proceeded with text and at least 1 space should be after it before it is replaced?

KISS is quite often nice in theory, but doesn't work in practice....

Like, I wanted to kiss that girl, but she slapped me :p
.-= A 'frrrrrrrituurrr' for Wacky =-.
05-03-2007 10:50 PM
Profile PM Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: RE: Replacing "?"
quote:
Originally posted by CookieRevised
? What?!?

will turn into:

..? What..?!..?

with that reg expression.... Maybe something to take into account that the question mark must be proceeded with text and at least 1 space should be after it before it is replaced?
No it won't. I assume you're talking about this one:

code:
stringObj.replace(/\?$/, "..?");

Because it is not global, only one occurrence will be replaced, and since there is the $ at the end of it, only the last one. "? What?!?" turns into "? What?!..?".
05-03-2007 10:57 PM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Replacing "?"
true, example given wasn't that great. But then again... the string:

"Hello? Anybody there?" will not be changed to "Hello..? Anybody there..?" either.
(and this string does pass his criterium of '?' being at the end of a sentence)

Just as "Hello!?" has a "?" at the end of the sentence. Does that mean it should be changed to "Hello!..?", "Hello..!?" or not at all?

So the point I was making before still applies... Before giving people a solution you must make sure you know exactly what they want, and simple solutions aren't always (in many cases never) the correct solutions. You need to take in account a lot of things....

Or state the limitations of the given solution...

;)

This post was edited on 05-03-2007 at 11:06 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
05-03-2007 11:01 PM
Profile PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Replacing "?"
What I did was fixing Ezra's given solution, I did not take the actual suggestion in account, and maybe that was a mistake from my side. Sorry for that.

Well, in that case the regular expression becomes more complicated:
code:
stringObj.replace(/\w\?(\s|$)/, "..?$1");
This example will replace all "?" with "..?" when they are preceded by an alphanumeric character (a-z, A-Z and 0-9) and followed by a white space character (new line, tab, space,...) OR the end of the string. Therefore, it's somewhat limited because it won't parse something like "Hello!?", but if you really need that too this can be fixed by using:
code:
stringObj.replace(/\w([!\?]+)(\s|$)/, "..$1$2");
but then, it'll also change "Hello!" into "Hello..!" and maybe you don't want that. To avoid that from happening, you'll need to go even further, but I'm not in the mood to do that now. :P

This post was edited on 05-04-2007 at 04:41 PM by Matti.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
05-04-2007 04:40 PM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On