Coldfusion - 从列表中删除所有非数字值?

时间:2011-01-14 19:37:19

标签: list coldfusion

我有一个通过URL传递的ID列表。我想在根据这些ID运行查询之前进行一些清理。我有这个:

<cfset URL.ID = ReReplaceNoCase(URL.ID,"[^0-9]","","ALL")>

但我意识到这也剥掉了逗号。有没有一种简单的方法可以从Coldfusion中的列表中删除非数字值?

4 个答案:

答案 0 :(得分:21)

为什么不在您的正则表达式中添加逗号?

<cfset URL.ID = ReReplaceNoCase(URL.ID,"[^0-9]","","ALL")>

变为

<cfset URL.ID = ReReplaceNoCase(URL.ID,"[^0-9,]","","ALL")>

答案 1 :(得分:4)

<cfscript>
//Test Case
URL.ID= "abc,12,3,zoiui fdsoaiu ,323333";

//URL.ID= "12,3,323333"
URL.ID= reReplace( URL.ID , "([^0-9,]+,)" , "" , "all" );

</cfscript>

话虽如此,你想把它放在<cfqueryparam .. list= "true" />

答案 2 :(得分:3)

正则表达式仍然在字符串中留下额外的逗号并接受部分数字,而是使用带整数验证的循环:

<cfset url.id = "100,abc,102z,eee,22.5,773">
<!--- 100,,102,,225,773 --->
<cfoutput>#ReReplaceNoCase(URL.ID,"[^0-9,]","","ALL")#<br /></cfoutput>

<cfset dirtyIds = listToArray(url.id)>
<cfset cleanIds = []>
<cfloop array="#dirtyIds#" index="dirtyId">
    <cfif isValid("integer",dirtyId)><cfset arrayAppend(cleanIds, dirtyId)></cfif>
</cfloop>
<cfset url.id = arrayToList(cleanIds)>
<!--- 100, 773 --->
<cfoutput>#url.id#</cfoutput>

答案 3 :(得分:0)

@orangepips

isNumeric()也可以正常工作。

<cfset url.id = "100,abc,102z,eee,22.5,773">
<cfset variables.newlist = ''>
<cfloop list="#url.id#" index="i">
    <cfif isNumeric(i)>
        <cfset variables.newlist = ListAppend(variables.newlist,i)>
    </cfif>
</cfloop>
<cfoutput>#variables.newlist#</cfoutput>