Shoutbox

[Request] A simple regular expression function - 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: [Request] A simple regular expression function (/showthread.php?tid=83432)

[Request] A simple regular expression function by Skarbo on 04-28-2008 at 09:33 AM

Im not any good at building regular expression so i need a litle help with this simple function.

All i want to do is search for a character in a string and move it to the end of the string.

Example:

function moveToEnd(string, search) ->
moveToEnd(AABBCCDDEE, A)

returns

BBCCDDEEAA (moves all the A's to the end)


RE: [Request] A simple regular expression function by markee on 04-28-2008 at 11:14 AM

If they are all grouped together:

code:
StringObj.replace(/(.*)(A*)(.*)/i,"$1$3$2);

I personally can't think of a way to get all of them on the end however... This code might work but t is untested...

code:
var StringObj;
while(RegExp(".*A+[^A]+A*$","gi").exec(StringObj)){
    StringObj.replace(/(.*?)(A*)(.*)/i,"$1$3$2);
}


RE: [Request] A simple regular expression function by John Anderton on 04-28-2008 at 05:40 PM

Try this. Non regexp method though.
1) Take string (AABBCCDDEE) and char to replace (A)
2) Count instances of char to replace (A)
3) Create target string all chars not char to replace in string ie target string = "BBCCDDEE"
4) Append 'count' number of char to replace (ie As) at the end


Should be simple enough :-)


RE: [Request] A simple regular expression function by markee on 04-28-2008 at 08:48 PM

quote:
Originally posted by John Anderton
2) Count instances of char to replace (A)
That would require a for loop that has more (or at least equal - only when there are no instances of double leters) itterations than my while though.... and would probably be a while satement unles you checked each letter individually.

However your method is better for someone to learn how to do it themselves I guess.....
RE: [Request] A simple regular expression function by Matti on 04-29-2008 at 04:43 PM

Looks like some very untested code, yes. I can't seem to get it working whatsoever, it's crashing my Messenger every time I try to run this (infinite loop?):

code:
var StringObj = "Some shit with A's.";
while(RegExp(".*A+[^A]+A*$","gi").exec(StringObj)){
    StringObj = StringObj.replace(/(.*?)(A*)(.*)/i,"$1$3$2");
}
Could you try to find some time to get it working in some way? :)
RE: [Request] A simple regular expression function by Skarbo on 04-29-2008 at 04:52 PM

I made a simple function for now (before your post John Anderton ;))

code:
// Move substring to end of string
function moveToEnd(string, substring) {
    var moveToEndI = 0;
    while(string.indexOf(substring) > -1) {
        string = string.replace(substring, "");
        moveToEndI++;
    }
   
    for (;moveToEndI > 0; moveToEndI--)
        string = string + "" + substring;
    return string;
}

RE: [Request] A simple regular expression function by markee on 04-30-2008 at 09:39 AM

quote:
Originally posted by Mattike
Could you try to find some time to get it working in some way? :)
Got it working now

code:
function moveToEnd(oString,sub){
    var re = RegExp(".*?"+sub+"+(?!$)","gim");
    eString = oString;
    while(re.exec(oString) != null){
        eString = eString.replace(RegExp("(.*?)("+sub+"+)(.*)","gi"),"$1$3$2");
    }
    return eString;
}
This has FAR less itterations and should respond quicker.  Realy I should do error checking to make sure sub is only a single character, but if you are carful then there shouln't be any problem and will work faster.

I hope this helps :)