String.replace(/re/, replFN) does not count the index position properly for unicode characters
Hello!
In my project, I want to find emoticons in text and append them to a text flow as images. I use regular expression and with English alphabet, all works fine, but when I enter text in other languages, every letter gets counted twice:
var re:RegExp = /:\)/g;
var textEng:String = "Hello, :)";
var textRu:String = "Привет :)";
textEng.replace(re, replFN); // index is 7
textRu.replace(re, replFN); // index is 13
function replFN():String{
var a:Array = arguments;
trace(arguments[1]); //The index position in the string where the match begins.
return arguments[0];
}
Obviously it's a bug, and the workaround is to use textRu.indexOf(arguments[0]), and keep some lastIndex:int variable when iterating over many matches of the string. But do I need to report it on Jira?
privatevar lastIndex:int = 0;
private function replFN():String{
var a:Array = arguments;
var textToAdd:String = textRu.slice(lastIndex,textRu.indexOf(arguments[0]));
trace(textToAdd);
lastIndex = textRu.indexOf(arguments[0]) + arguments[0].length;
trace(arguments[0]);
return arguments[0];
}
Thanks.