如果您想替换每一行中的某些内容,并且只需加入结果,则无需将其拆分为行。您可以只使用 replaceAll 方法。
replaceAll("pattern to search","string to put where pattern match");
如果您仍然需要替换并加入所有行,您可以执行以下操作
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
}
如果您仍想使用 match(),它会返回所有匹配项而不是单个字符串:
var str:String = "bob@example.com, omar@example.org";
var pattern:RegExp = /\w*@\w*\.[org|com]+/g;
var results:Array = str.match(pattern);
// the result is ["bob@example.com","omar@example.org"]
所以,如果你有这样的东西,你可以做一个 for 来迭代所有的匹配。
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;
}