Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am able to trace the result without any problem. however i would like to convert the result into a string and not an Array. the reason i want to do this is because i need to do a search and replace on each string that is returned.Also can someone help to put each line of the array in a string so i can do the search and replace.Sorry guys i am a newbie in flex dev. thank you very much for your help

 var myArrayOfLines:Array = ul.data.split(/\n/);
 var line:String;
 for each (var lineRaw:String in myArrayOfLines)
 { 
  //line:String = lineRaw.match(pattern);
 trace(lineRaw.match(pattern));
  }
share|improve this question

2 Answers

Since your array stores strings.You can define String variable(for example str) and the iterate the array and append each item of the array to str like this:

var str : String = "";
for each (var line : String in myArrayOfLines) {
    str += line;
}
//you can do some match and replace operation on the str variable, 
//since it contains all the elements of the arr.
share|improve this answer

If you want to replace something in each line, and just join the result, you don't need to split it in lines. You can just use the replaceAll method.

replaceAll("pattern to search","string to put where pattern match");

If you still need to replace and join all the lines, you can do something like this

    var myArrayOfLines:Array = ul.data.split(/\n/);
    var line:String;
    var result:String = "";
    for each (var lineRaw:String in myArrayOfLines){ 
        // you can use replace or replaceAll
        line:String = lineRaw.replace(pattern, "whatever you want to put in places");
        result += line;
        result += "\n"; // use this line if you want to add the break lines again
    }

If you want to still use the match(), it returns all the matches and not a single string:

var str:String = "[email protected], [email protected]"; 
var pattern:RegExp = /\w*@\w*\.[org|com]+/g; 
var results:Array = str.match(pattern);
// the result is ["[email protected]","[email protected]"]

So, if you have something like this, you can do a for to iterate all the matches.

    var lines:Array = lineRaw.match(pattern);
    for each (var line:String in lines){ 
        // you can do whatever you want with the String line
        result += line;
    }
share|improve this answer
i really appreciate your help but i am still not able to do a match in a string using regex: for each (var lineRaw : String in myArrayOfLines) { str = lineRaw; str = str.match(pattern); trace("line:"+str); } – user1960170 Jan 11 at 3:57
both answers were very helpful, was ble to get the string worked out. thank you very much :). – user1960170 Jan 12 at 23:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.