What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Problem with RegExp + Replace

Pages: (2): « First « 1 [ 2 ] Last »
Problem with RegExp + Replace
Author: Message:
Shondoit
Full Member
***

Avatar
Hmm, Just Me...

Posts: 227
Reputation: 15
35 / Male / Flag
Joined: Jul 2006
RE: Problem with RegExp + Replace
He had the right solution
Yours doesn't take into account for more numbers (as you said so yourself)

This should work, in red are the changes:
code:
var reg_exp = /([0-9*/+\-()]+)/gi;
//brackets indicate a set of characters, you don't need to escape anything in here except for the minus sign, which would indicate a range of characters
document.write(Messege.replace(reg_exp,"[$1]"));


Especialy the little plus at the end of the RegExp is important because it indicates that it need one or more matches..., instead of the asterisk which indicates zero or more matches, so in this case, every single character. That's why you had that problem...

If you have any further question or want something explained, just PM me
My scripts:                            [Image: shondoit.gif]
+ Timezone
+ Camelo
+ Multisearch
09-28-2006 04:57 AM
Profile PM Find Quote Report
Montago
Junior Member
**


Posts: 25
Joined: Sep 2006
O.P. RE: Problem with RegExp + Replace
thanks Shondoit - works a bit better

now i need to refine the regexp to capture mathpieces better - currently smileys are also captured because of ')'

so im thinking that a math piece is constructed by:
- a number of brackets '()' zero or more
- at least 2 numbers
- at least one operator '/*-+^'

a math rule is that 2(5+4) == 2*(5+4) so this one needs to be captured aswell

a better choice then looking for mathpieces is to let the user mark a piece himself... with corner brackets '[]' which is many times simpler

Next - how do i EVAL the piece ?? earlier in the thread, a function was written - this is a bit unhandy - and i recall that the function would only process one math piece ?
09-28-2006 11:14 AM
Profile E-Mail PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Problem with RegExp + Replace
quote:
Originally posted by Montago
Next - how do i EVAL the piece ??
like exactly like you just said: use the Eval() function in the replace.

quote:
Originally posted by Montago
and i recall that the function would only process one math piece ?
the replace can replace all occuring findstrings.

Search the forum for "calc", there are already a few threads about calculating  expressions. So to avoid repeating everything which has been said already in other threads, search forums first and you'll find similar threads..

- calculating with eval(). eg:
CookieRevised's reply to An idea for a script

- one which explanes how the Replace method actually works (instead of "$1" as replacement string, you can use a function as a replacement string. This is what makes Replace so powerfull and which makes it possible to use Eval for example directly in your replace method. eg:
CookieRevised's reply to [Question] Isn't string.replace supposed to replace all occurences ?
(^^ explains the replace function)
CookieRevised's reply to Replace Colour Codes
(^^ shows an example for replace using a function as replace text)
CookieRevised's reply to [I help them] VB2JS
(^^ shows an example for replace using a function as replace text)

- one which uses a complex algorithm instead of eval() to calculate almost all possible expressions (eval() is rather limited). eg:
[?] Pow... err, triggering some function on certain convo events?

- etc...

This post was edited on 09-29-2006 at 12:39 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-29-2006 12:25 AM
Profile PM Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Problem with RegExp + Replace
Note that you can't do:
code:
Str.replace(RegExp,eval("$1"));
09-29-2006 12:34 AM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Problem with RegExp + Replace
quote:
Originally posted by deAd
Note that you can't do:
code:
Str.replace(RegExp,eval("$1"));

if that was in reply to my post, then read the threads I linked to, or look up the information on the replace function in the JScript help files (on msdn, in the JScript help file, etc)...

This post was edited on 09-29-2006 at 12:50 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-29-2006 12:41 AM
Profile PM Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Problem with RegExp + Replace
It wasn't in reply, it was just a note :P
EDIT: Also, I posted an example of using a function in String::replace earlier in this thread...just that my regex was a bit off..

This post was edited on 09-29-2006 at 12:44 AM by deAd.
09-29-2006 12:43 AM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Problem with RegExp + Replace
quote:
Originally posted by deAd
It wasn't in reply, it was just a note :P
EDIT: Also, I posted an example of using a function in String::replace earlier in this thread...just that my regex was a bit off..
your function was a bit off too...

it worked but you let replace search with a regular expression and when it found a match you searched the string again for that same match, while it already found the match in the first place. In short: your function didn't made much sense logically (aka doesn't teach the proper thing). What you did was like asking a doctor to examine you, and when he lists the problems, you say: screw it, I'll search it myself using that medical reference... :p

;)

correct way:

code:
var s = "Hello there, 1+1 should be 2. And 5-2 will be 3.";
s = s.replace(/([0-9*/+\-()]+)/gi, function($0, $1) { return eval($1) });
Debug.Trace(s);

This post was edited on 09-29-2006 at 12:59 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-29-2006 12:51 AM
Profile PM Find Quote Report
Montago
Junior Member
**


Posts: 25
Joined: Sep 2006
O.P. RE: Problem with RegExp + Replace
var s = "Hello there, 1+1 should be 2. And 5-2 will be 3.";
s = s.replace(/([0-9*/+\-()]+)/gi, function($0, $1) { return eval($1) });
Debug.Trace(s);

did the trick :)

now im able to do some more advanced stuff upon those expressions that are found...

also not-find simple chars... when i write a bare number, it is eval'ed so i gotta fix this :)
09-29-2006 09:33 AM
Profile E-Mail PM Find Quote Report
Pages: (2): « First « 1 [ 2 ] Last »
« 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