Nick, 

 

I think this satisfies your conditions. There is a for-loop but it's not
iterating over the entire string, which, I'm certain, is what you wanted to
avoid. 

 

var myString:String = 'flex "action script" parse string function'; 

var resultArr:Array = regExpDQ(myString, false); 

trace("resultArr = ",resultArr); 

 

function regExpDQ(s:String, includeQuotes:Boolean):Array { 

                var re:RegExp = /".*?"/g; 

                // get string segments contained in double-quotes  

                var matchArr:Array = s.match(re); 

                // strip out string segments contained in double-quotes 

                s = s.split(re).join(''); 

                // put the remaining string segments into an array  

                var arr:Array = s.split(' '); 

                // merge the two arrays  

                var len:uint = arr.length; 

                var a:Array = []; 

                var j:uint = 0; 

                for(var i:uint = 0; i < len; i++) { 

                                if(arr[i] != ""){ 

                                                a[i] = arr[i]; 

                                }else{ 

                                                if(includeQuotes){ 

                                                                a[i] =
matchArr[j++]; 

                                                }else{ 

                                                                a[i] =
matchArr[j++].split('"').join(""); 

                                                } 

                                } 

                } 

                return a; 

} 

 

Regards,

 

Keith Reinfeld
Home Page:  <http://keithreinfeld.home.comcast.net/>
http://keithreinfeld.home.comcast.net

 

From: flexcoders@yahoogroups.com [mailto:flexcod...@yahoogroups.com] On
Behalf Of Nick Middleweek
Sent: Wednesday, July 28, 2010 12:20 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] How to split a String on spaces but including Quoted
Phrases?

 

  

Hi,

I haven't been able to work out regex yet so apart from looping and parsing
a string manually, is there anyway of parsing the following string...

flex "action script" parse string function

into the following Array...

[0] = flex
[1] = action script
[2] = parse
[3] = string
[4] = function


Thanks,
Nick



<<image001.jpg>>

<<image002.jpg>>

Reply via email to