Problem with RegExp + Replace - 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: Problem with RegExp + Replace (/showthread.php?tid=66726) Problem with RegExp + Replace by Montago on 09-27-2006 at 08:28 PM
i want to do something really simple - to locate numbers for calculation RE: Problem with RegExp + Replace by deAd on 09-27-2006 at 08:57 PM
Try using: code: Also, I don't know a ton about regular expressions, but that one looks a bit flawed to me. RE: Problem with RegExp + Replace by Silentdragon on 09-27-2006 at 09:24 PM
code:Should work deAd: $1-9 won't work unless you use (). So for each $ you need a set of (). RE: Problem with RegExp + Replace by deAd on 09-27-2006 at 09:47 PM 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.. RE: Problem with RegExp + Replace by Montago on 09-27-2006 at 10:45 PM
quote: 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 RE: Problem with RegExp + Replace by deAd on 09-27-2006 at 10:49 PM 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...). RE: Problem with RegExp + Replace by Montago on 09-27-2006 at 10:54 PM
ohhh... that might explain something... if the ')' is un-escaped, it might make a submatch... RE: Problem with RegExp + Replace by deAd on 09-27-2006 at 11:03 PM
Yes RE: Problem with RegExp + Replace by Montago on 09-27-2006 at 11:09 PM
i want to find calculations within a text string... RE: Problem with RegExp + Replace by deAd on 09-27-2006 at 11:54 PM
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: RE: Problem with RegExp + Replace by Shondoit on 09-28-2006 at 04:57 AM
He had the right solution code: 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 RE: Problem with RegExp + Replace by Montago on 09-28-2006 at 11:14 AM
thanks Shondoit - works a bit better RE: Problem with RegExp + Replace by CookieRevised on 09-29-2006 at 12:25 AM
quote:like exactly like you just said: use the Eval() function in the replace. quote: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... RE: Problem with RegExp + Replace by deAd on 09-29-2006 at 12:34 AM
Note that you can't do: code: RE: Problem with RegExp + Replace by CookieRevised on 09-29-2006 at 12:41 AM
quote: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)... RE: Problem with RegExp + Replace by deAd on 09-29-2006 at 12:43 AM
It wasn't in reply, it was just a note RE: Problem with RegExp + Replace by CookieRevised on 09-29-2006 at 12:51 AM
quote: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... correct way: code: RE: Problem with RegExp + Replace by Montago on 09-29-2006 at 09:33 AM
var s = "Hello there, 1+1 should be 2. And 5-2 will be 3."; |