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:
Montago
Junior Member
**


Posts: 25
Joined: Sep 2006
O.P. Problem with RegExp + Replace
i want to do something really simple - to locate numbers for calculation

function...
    var reg_exp = "([0-9\*\/+\-()]*)";

    if( Messege.match( new RegExp(reg_exp) ) ){
        return Messege.replace(new RegExp(reg_exp,'gi'),"[$1]");
    }

...

Using the function above, should do this :

input : "Havelåge 5+5+5-(5*6) sdsd"
output : "Havelåge [5+5+5-(5*6)] sdsd"

but instead it : "[]H[]a[]v[]e[]l[]å[]g[]e[] [5][]+[5][]+[5][]-[]([5][]*[6][])[] []s[]d[]s[]d[]"


what am i doing wrong ???
09-27-2006 08:28 PM
Profile E-Mail PM Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Problem with RegExp + Replace
Try using:
code:
return Message.replace(/[0-9\*\/+\-()]*/g, "[$1");

Also, I don't know a ton about regular expressions, but that one looks a bit flawed to me.
09-27-2006 08:57 PM
Profile PM Find Quote Report
Silentdragon
Full Member
***

Avatar
if(life==null && wrists) EmoAlert();

Posts: 148
Reputation: 2
34 / Male / –
Joined: Jun 2006
RE: Problem with RegExp + Replace
code:
Message.replace(/([0-9+\-()*\\\/]+)/,"[$1]")
Should work

deAd: $1-9 won't work unless you use (). So for each $ you need a set of ().

This post was edited on 09-27-2006 at 09:26 PM by Silentdragon.
09-27-2006 09:24 PM
Profile E-Mail PM Web Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Problem with RegExp + Replace
Well there were parentheses, but they would always return a blank string -- that's why I said it looked flawed...but I didn't try to correct it because (1) not exactly sure what they're trying to accomplish, and (2) since I'm not so good at regular expressions, I figured I'd just leave it as it was..
09-27-2006 09:47 PM
Profile PM Find Quote Report
Montago
Junior Member
**


Posts: 25
Joined: Sep 2006
O.P. RE: Problem with RegExp + Replace
quote:
Originally posted by Silentdragon
code:
Message.replace(/([0-9+\-()*\\\/]+)/,"[$1]")
Should work


no way :)

/([0-9+\-()*\\\/]+)/

'+' after numeric gives trouble
')*' will look for more ')'
'\\' why look for backlash ?
']+' i guess *(start) is more appropriate ??

you are right about the () => $1 matching, which is what im looking for

i tried you solution didn't do what i wanted though, actually nothing... im trying a modified version

thanks anyway :)
09-27-2006 10:45 PM
Profile E-Mail PM Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Problem with RegExp + Replace
Actually, ")*" will not look for more ")"s, unless you specify that the ")" in that case is a character, and in that expression ")" is being used to end a sub-match (whatever you call it...).
09-27-2006 10:49 PM
Profile PM Find Quote Report
Montago
Junior Member
**


Posts: 25
Joined: Sep 2006
O.P. RE: Problem with RegExp + Replace
ohhh... that might explain something... if the ')' is un-escaped, it might make a submatch...

so, i need to escape the ')' :)
09-27-2006 10:54 PM
Profile E-Mail PM Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Problem with RegExp + Replace
Yes :)

EDIT: What exactly are you trying to do?

This post was edited on 09-27-2006 at 11:03 PM by deAd.
09-27-2006 11:03 PM
Profile PM Find Quote Report
Montago
Junior Member
**


Posts: 25
Joined: Sep 2006
O.P. RE: Problem with RegExp + Replace
i want to find calculations within a text string...

lets say i write
"hello jim, i have 25*20 dollars"
then my plugin calculated the amount -->
"hello jim, i have 500 dollars"

so i want to match simple math pieces
09-27-2006 11:09 PM
Profile E-Mail PM Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Problem with RegExp + Replace
Well then I think you're going about it wrong. Here is a better way to do it. The code, however, is a bit messy. This will evaluate division, multiplication, addition, and subtraction (/, *, +, -). It uses the eval() function. It accepts decimals and disregards whitespace. However, there is only support for problems with two numbers (3*4*5 won't work).

code:
var Str = "Did you know that 5 time 5 is 5*5? Did you know that 8 plus 8 is 8+8?";

    var RegExp = /[\d.]+[\s]*[*/+-][\s]*[\d.]+/g;

    Str = Str.replace(RegExp, function(){ var res = eval(RegExp.exec(Str)[0]); RegExp.lastIndex++; return res; } );

This post was edited on 09-28-2006 at 12:14 AM by deAd.
09-27-2006 11:54 PM
Profile 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