Cold Fusion 9 - Remove JSON string from text

168 Views Asked by At

I am generating a preview of certain length out of a text string. The text was made out of a HTML string where the HTML code has been removed. Because of certain reasons there are some JSON block within the text. These JSON blocks are placeholders to retrieve information from a database and replace the json string with it on page load.

For the preview the JSON must not be in the string. Therefore I have to clean the string and remove the JSON blocks.

Here is an example of how the string may look:

Pellentesque et vulputate felis. {"bla":"blabla", "blubb":"blubablub"} Maecenas tortor ex, commodo eu massa a, vehicula cursus erat. Nam rhoncus, nunc ut lobortis pretium, libero lorem {"blurb":"blarblar", "blabb":"blabablurb", "test":"testatest"} facilisis urna, et gravida tellus turpis ut nisi. Nulla in ullamcorper metus. Sed sed blandit magna. Integer fermentum.

How do I get these two JSON blocks using regex and remove it?

{"bla":"blabla", "blubb":"blubablub"}
{"blurb":"blarblar", "blabb":"blabablurb", "test":"testatest"}

It works with Rematch() and a following cfloop over the array of JSON blocks. But is it possible with ReReplace()?

Just found the solution

ReReplace(mystring, "\{([^}]*)\}", "", "ALL")

Sry for bothering.

1

There are 1 best solutions below

0
On

Just found the solution

ReReplace(mystring, "\{([^}]*)\}", "", "ALL")

This solution doesn't work for JSON with nested objects but in my case it's enough.