ActionScript2: Split a text with two delimiter, Help


I try to create a project with use a Split in AC2.

this I have

    in_pers = 3x5+5x3+6x8;

var tmp:Array = in_pers.split("+");

trace(tmp);  //output ==  3x5, 5x3, 6x8 

but, how if in_pers = 3×5+5×3-6×8-4×2;

how can I get the result 3×5, 5×3, 6×8, 4×2

how to use the split with two delimiter.

Best regard..

Best Solution

There is a much simpler way to do what wikiup suggested:

function strReplace( haystack:String, needle:String, replacement:String ):String
    var tmpA:Array = haystack.split( needle );
    return tmpA.join( replacement );

function multiSplit( haystack:String, needles:Array ):Array
    // generate a unique String for concatenation.
    var salt:String = String( Math.random() ); //replace with anything you like

    // Replace all of the strings in the needles array with the salt variable
    for( var i:Number = 0; i < needles.length; i++ )
        haystack = strReplace( haystack, needles[ i ], salt );

    //haystack now only has the objects you want split up concatenated by the salt variable.
    //split haystack and you'll have your desired result.
    return haystack.split( salt );

Not only is this smaller, it is also faster (while takes a lot longer than for( i = 0; i < val; i++ ) (look up iteration vs. recursion) ).