Replace each RegExp match with different text in ActionScript 3
I would like to know how to replace each match with a different text? Let's say the original text:
var strSource:String = "find it and replace what you find.";
.. and we have a regex like:
var re:RegExp = /\bfind\b/g;
Now I need to replace each match with a different text (for example):
var replacement:String = "replacement_" + increment.toString();
Thus, the result will be something like this:
output = "replacement_1 it and replace what you replacement_2";
Any help is appreciated.
0
a source to share
4 answers
I finally came up with a solution. Here it is, if anyone needs it:
var re:RegExp = /(\b_)(.*?_ID\b)/gim;
var increment:int = 0;
var output:Object = re.exec(strSource);
while (output != null)
{
var replacement:String = output[1] + "replacement_" + increment.toString();
strSource = strSource.substring(0, output.index) + replacement + strSource.substring(re.lastIndex, strSource.length);
output = re.exec(strSource);
increment++;
}
Thanks anyway...
+1
a source to share