如何在ActionScript 3中替换闭括号和左括号之间的逗号

时间:2015-01-06 15:39:42

标签: mysql regex actionscript-3 flex esri

此问题的基础来自FLEX地图应用程序,其中我使用窗口小部件中的复选框为在要素图层中提供的地理空间数据库中的多个字段生成查询字符串。在程序为数据库中的每个可用字段编译每个查询后,我不得不连接每个查询。这在功能结束时显示如下。这是循环复选框,生成查询字符串,并尝试连接查询字符串的函数:

public function compileDefexp():void {
    //conFilter is an HBox that contains the available field names in checkboxes.
    var countfilt:uint = conFILTER.numElements;
    var defExpGEO_ACC:String = "";
    var defExpACT_TYP:String = "";
    var defExp:Array = [];

    //clearing filterList on each checkbox click
    filterList = [];

    //loop through the field and field value checkboxes to generate the query strings
    for(var i:int = 0;i < countfilt; i++){
        var childFilter:Object = conFILTER.getElementAt(i);
        var filterName:String = childFilter.name;
        if(childFilter.selected){
            filterList.push(filterName);
            if(filterName == "GEO_ACC"){
                compileValuelistgeo();
                defExpGEO_ACC = "(" + filterName + ") IN (" + qValuelistGEO_ACC + ")";
                defExp.push(defExpGEO_ACC);
            }else if(filterName == "ACT_TYP"){
                compileValuelistact();
                defExpACT_TYP = "(" + filterName + ") IN (" + qValuelistACT_TYP + ")";
                defExp.push(defExpACT_TYP);
            }
        }
    }

    var defExpStr:String = defExp.toString();
    var countfilterList:uint = filterList.length;
    var filterListstr:String = filterList.toString();

    //Cannot get replace the comma between the closed and open parentheses with AND
    //We need to accomplish this to concatenate the query strings for the definition
    //expression to execute correctly.

    //Tried to use RegExp
    var strPattern:RegExp = /\),\(/g;
    var strReplace:RegExp = /\) AND \(/g;

    defExpStr.replace(strPattern, strReplace);

    geoPoints.definitionExpression = defExpStr;

    trace(defExpStr); //(GEO_ACC) IN (GA Value 1,GA Value 2),(ACT_TYP) IN (AT Value 1)

    //We need this to be:
    //(GEO_ACC) IN (GA Value 1,GA Value 2) AND (ACT_TYP) IN (AT Value 1)
}

2 个答案:

答案 0 :(得分:0)

你可以使用这样的替换功能:

    public function replaceExample(str:String, patternOld:String,  patternNew:String):String
    {
        var str2:String = str.replace(patternOld,patternNew);
        return str2;
    }

  //Test result will be:(GEO_ACC) IN (GA Value 1,GA Value 2) AND (ACT_TYP) IN (AT Value 1)

 var str:String = "(GEO_ACC) IN (GA Value 1,GA Value 2),(ACT_TYP) IN (AT Value 1)";
 Alert.show( replaceExample(str, "),(", ") AND (") );

答案 1 :(得分:0)

我使用if语句进行了一些实验,并利用数组的索引根据所选的过滤器复选框的数量一起编译查询语句。本质上,我们不会尝试替换数组中的逗号,而是将每个查询语句与数组的索引号连接起来。在编译每个查询列表之后放置以下代码:

var defExpStr:String;
if (filterList.length == 1){
    defExpStr = defExp[0];
}else if (filterList.length == 2){
     defExpStr = defExp[0] + " AND " + defExp[1];
}

geoPoints.definitionExpression = defExpStr;
geoPoints.visible = true;

总之,如果您需要一个允许最终用户在数据库中选择多个字段和值并使用复选框配置查询的工具,则此代码将执行此操作。此代码专为ArcGIS API for FLEX开发,适用于通过ArcServer提供服务的地理空间数据。