Coldfusion - remove all non-numeric values ​​from a list?

I have a list of identifiers passing through a URL. I want to do some disinfection before running a query based on these identifiers. I have it:

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

But I understand that I am also removing the comma. Is there an easy way to remove non-numeric values ​​from a list in Coldfusion?

+8
list coldfusion
source share
4 answers

Why not just add a comma to your regex?

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

becomes

 <cfset URL.ID = ReReplaceNoCase(URL.ID,"[^0-9,]","","ALL")> 
+20
source share
 <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> 

Having said that, you want to put this in <cfqueryparam .. list= "true" />

+4
source share

Regex still leaves extra commas in the string and accepts partial numbers, instead I would use an integer-checking loop:

 <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
source share

@orangepips

isNumeric () should also work.

 <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> 
0
source share

Source: https://habr.com/ru/post/651083/


All Articles