Skip to content


String Replace Function in Flex

I’m not sure if this is a redundant function or not. I know that Flex String objects have a replace() method but as far as I can tell this only replaces the first match and not every match in a string.

Here is an actionscript function for flex which works the same as the php str_replace() function.

str_replace( needle, replacement, haystack ) in ActionScript

public function str_replace(needle:String, replacement:String, haystack:String):String{  var strArr:Array = haystack.split(needle);  return strArr.join(replacement);}

If this is a pointless function and there is an easier way to accomplish this built into flex I would love to hear about it. I looked over the language reference documents and couldn’t find something.

Posted in Code.

Tagged with , .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. John says

    You can also use the global flag g at the end of your regular expression.

  2. Anonymous says

    var myPattern:RegExp = /sh/g;
    var str:String = “She sells seashells by the seashore.”;
    trace(str.replace(myPattern, “sch”));



Some HTML is OK

or, reply to this post via trackback.



close