Skip to main content
zavr
Participant
July 3, 2014
Question

String.replace(/re/, replFN) does not count the index position properly for unicode characters

  • July 3, 2014
  • 1 reply
  • 429 views

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.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
July 3, 2014

use the flash string/array methods.  they are faster, more reliable and easier to debug.

zavr
zavrAuthor
Participant
July 4, 2014

I am using the Flash String method. It's called replace... and there's a bug.

kglad
Community Expert
Community Expert
July 4, 2014

ok.

don't use regular expressions.  use the other flash string/array methods.  they are faster, more reliable and easier to debug.