按点分割的Flex不起作用

时间:2012-11-06 10:34:54

标签: regex flex

我在flex中有一个文本框,我试图分割用户输入的数量。代码类似于:

var splitAmount:Array = toAmountLocal.split("\\.");

尝试使用点(。)的不同选项,但没有任何工作,每次只返回splitAmount.length

2 个答案:

答案 0 :(得分:1)

如果使用String作为split方法的参数,则不必转义任何内容;只是做:

toAmountLocal.split(".");

但是如果你想使用正则表达式作为参数,那么你只需要用一个反斜杠来转义点,如下所示:

toAmountLocal.split(/\./);

答案 1 :(得分:0)

下面的代码可以帮助您:我已添加了您在逻辑中缺少的注释。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="init()">

    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;

            [Bindable]
            private var toAmountLocal:String = "123.45.6.78";

            private function onClickHandler():void
            {
                //if user is entering value your local variable should be updated.
                toAmountLocal = inputID.text;
                var splitAmount:Array = toAmountLocal.split('.');
                Alert.show(splitAmount.length.toString())
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:layout>
        <s:VerticalLayout/>
    </s:layout>

    <s:TextInput id="inputID" text="{toAmountLocal}"/>
    <s:Button label="Split" click="onClickHandler()"/>

</s:Application>