1

Let's say I have:

<cfscript>
arrButtons = [
    {
        "name" = "Add",
        "bclass" = "add",
        "onpress" = "addItem"
    },
    {
        "name" = "Edit",
        "bclass" = "edit",
        "onpress" = "editItem"
    },
    {
        "name" = "Delete",
        "bclass" = "delete",
        "onpress" = "deleteItem"
    }
];

jsButtons = SerializeJSON(arrButtons);
// result :
// [{"onpress":"addItem","name":"Add","bclass":"add"},{"onpress":"editItem","name":"Edit","bclass":"edit"},{"onpress":"deleteItem","name":"Delete","bclass":"delete"}]
</cfscript>

For every onpress item, I need to remove the double quotes from its value to match the JS library requirement (onpress value must a callback function).

How do I remove the double quotes using a regular expression?

The final result must be:

[{"onpress":addItem,"name":"Add","bclass":"add"},{"onpress":editItem,"name":"Edit","bclass":"edit"},{"onpress":deleteItem,"name":"Delete","bclass":"delete"}]

No double quotes surrounding addItem, editItem, and deleteItem.

Edit 2012-07-13 Why I need this? I created a CFML function that the result is a collection of JS that will be used in many files. jsButton object will be used as one part of the options available in the JS library. One of that function's arguments is an array of struct (the default is arrButtons), and the supplied arguments value can merge with the default value.

Since we can't (in CFML) write onpress value without double quotes, so I have to add double quotes to that value, and convert the (CFML) array of struct to JSON (which is just a string) and remove the double quotes before place it in the JS library option.

with Railo, we can declare the struct as a linked struct to make sure we have same ordered key for loop or conversion (from above example onpress always the latest key in the struct). with this linked struct and same key order, we can remove the double quotes with simple Replace function, but of course we can't guarantee every programmer who use the CFML function doesn't forget to use linked struct and key order same as example above

tsurahman
  • 1,892
  • 5
  • 17
  • 26
  • You have to read the value and give the callback correspondingly. Regex is totally NOT involved. – nhahtdh Jul 12 '12 at 04:31

3 Answers3

1

I'm not sure this is actually necessary - depending on how/where you're dealing with the JS callbacks, it might be possible to use the string function names to reference the function without needing to remove the quotes (i.e. object[button.onpress]).

However, since you asked, here is a regex solution:

jsButtons  = jsButtons.replaceAll('(?<="onpress":)"([^"]+)"','$1');


The regex there is made up of two parts:

(?<="onpress":) -- lookbehind to ensure we are dealing with the text "onpress":
"([^"]+)" -- match the quotes and capture their contents.

The $1 on the replacement side is to replace the matched text (i.e. the entire quoted value) with the first capture group (i.e. the contents of the quotes).

If case-sensitivity of "onpress" might be an issue, you can prefix the regex with (?i) to ignore case.

If there will be multiple different events (not just "onpress") you can update the relevant part of the expression above to be (?<="on(?:press|hover|squeek)":) etc.


Note: All the above relies on the format output from serializeJson not changing - if it's possible that there might be comments, whitespace, single quotes, or anything else in future then a longer expression would be needed to cater for those - which is part of why you should investigate if you even need regex to solve this problem in the first place.

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
1

What you're wanting to output is not JSON, so using SerializeJSON is a kludge.

Is there any reason you are putting it into a ColdFusion Array first, instead of writing the Javascript directly?

JSON is purely meant to be a data description language. Per http://www.json.org, it is a "lightweight data-interchange format." - not a programming language.

Per http://en.wikipedia.org/wiki/JSON, the "basic types" supported are:

  • Number (integer, real, or floating point)
  • String (double-quoted Unicode with backslash escaping)
  • Boolean (true and false)
  • Array (an ordered sequence of values, comma-separated and enclosed in square brackets)
  • Object (collection of key:value pairs, comma-separated and enclosed in curly braces)
  • null

--Source

Community
  • 1
  • 1
nosilleg
  • 2,143
  • 1
  • 22
  • 36
0

I guess in this case you can simply use serialize(). That should do the trick...

Gert