Coldfusion remove square brackets from string

Panos used Ask the Experts™
Hello experts.
I need help to remove square brackets from a string
Any help?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015

If you just want to remove the brackets, a basic replace() would work:

<cfset theText = "This is [some text] with [brackets]">
<cfset newText =  Replace(Replace(theText, ']', '', 'all'), '[', '', 'all')>
newText = <cfoutput>#newText#</cfoutput>

Open in new window

Otherwise, can you give an example of the original text, and desired result?


Hi agx.
From my last question i have a binary response from <cfhttp call and using charsetEncode(response.filecontent, "utf-8")>  i have output like this:

I m trying to get only the list of products and loop over the list and insert the values in my database.
Using the rereplace i did delete the '{"product": '  and  ',"keyMap":"products:shampoo"}''
But wth the brackets it didn t work. i got the error:
 Malformed regular expression "[".
Reason: Unmatched [] in expression..
error line: <cfset responseString = rereplace(responseString,"[","","ALL")/>

I don t know  if is the right way to do this. I m sure there is a better way.
Most Valuable Expert 2015
Edit:  Oh okay. Forget about the replace. The response string is JSON. Just deserialize the whole string and you'll get back CF objects you can loop through:

<!--- DEMO ONLY --->
<cfsavecontent variable="responseString">{"product":[{"productId":"87","productname":"Name1"},{"productId":"40","productname":"Name2"},{"productId":"486","productname":"Name3"}],"keyMap":"products:shampoo"}</cfsavecontent>

<cfset response = deserializeJSON(responseString)>
<cfdump var="#response#">

<!--- the "product" key contains an array of structures (with product details) --->
<cfloop array="#response.product#" index="product">
        <!--- DEMO: display ID and product name --->
        <!--- Do whatever you want with the id's and names here --->
	product = #product.productid# - #product.productName#<br>

Open in new window


Thank you very much agx.
Problem is solved.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial