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

How to replace a comma between a closed parenthesis and an open parenthesis in ActionScript 3

这个问题的基础来自一个 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)
}

您可以像这样使用替换功能:

    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 (") );

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

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;

总而言之,如果您需要一个允许最终用户 select 数据库中的多个字段和值并使用复选框配置查询的工具,则此代码可以做到这一点。此代码专为 ArcGIS API for FLEX 开发,适用于通过 ArcServer 提供服务的地理空间数据。