Filtering an XMLListCollection using Regular Expressions

The following example shows how you can filter an XMLListCollection collection by using regular expressions in Flex 4.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see To download the latest nightly build of the Flex 4 SDK, see

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

For more information on the the API changes related to the removal of the “fx” prefix in Flex 4, see the “Dropping the Fx Prefix” page on

<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<s:Application name="XMLListCollection_filterFunction_RegExp_test"
        <s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
            private function textInput_change():void {
                if (textInput.text.length == 0) {
                    xmlListColl.filterFunction = null;
                } else {
                    xmlListColl.filterFunction = xmlListColl_filterFunc;
            private function xmlListColl_filterFunc(item:Object):Boolean {
                if (textInput.text.length == 0) return true;
                var f:String = "ig";
                var wasRegExp:RegExp = new RegExp(textInput.text, f);
                var nowRegExp:RegExp = new RegExp(textInput.text, f);
                var wasMatch:Boolean = wasRegExp.test(item.@was);
                var nowMatch:Boolean = nowRegExp.test(item.@now);
                return (wasMatch || nowMatch);
        <fx:XML id="nodesXML" source="nodes.xml" />
        <mx:XMLListCollection id="xmlListColl"
                filterFunction="xmlListColl_filterFunc" />
        <mx:Form styleName="plain">
            <mx:FormItem label="Filter:">
                <s:TextInput id="textInput"
                        change="textInput_change();" />
    <mx:DataGrid id="dataGrid"
            <mx:DataGridColumn dataField="@was" headerText="Was:" />
            <mx:DataGridColumn dataField="@now" headerText="Now:" />

And the nodes.xml data file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- -->
    <node was="mx.components.ButtonBarButton" now="spark.components.ButtonBarButton" />
    <node was="mx.components.DataGroup" now="spark.components.DataGroup" />
    <node was="mx.components.DropDownList" now="spark.components.DropDownList" />
    <node was="mx.components.FxButton" now="spark.components.Button" />
    <node was="mx.components.FxButtonBar" now="spark.components.ButtonBar" />
    <!-- ... -->


View source is enabled in the following example.

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

3 thoughts on “Filtering an XMLListCollection using Regular Expressions

Comments are closed.