Shoutbox

Replacing "?" - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Replacing "?" (/showthread.php?tid=74131)

Replacing "?" by Jimbo on 05-03-2007 at 04:43 PM

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?


RE: Replacing "?" by Ezra on 05-03-2007 at 05:01 PM

That's possible with a simple regex.

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


RE: Replacing "?" by Jimbo on 05-03-2007 at 06:01 PM

oh ok :$
Thanks


RE: Replacing "?" by Matti on 05-03-2007 at 06:28 PM

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)
RE: Replacing "?" by Ezra on 05-03-2007 at 07:02 PM

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 :(


RE: Replacing "?" by CookieRevised on 05-03-2007 at 10:50 PM

? 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


RE: RE: Replacing "?" by deAd on 05-03-2007 at 10:57 PM

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?!..?".
RE: Replacing "?" by CookieRevised on 05-03-2007 at 11:01 PM

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...

;)


RE: Replacing "?" by Matti on 05-04-2007 at 04:40 PM

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