Loading ...
Sorry, an error occurred while loading the content.

Newbie question re: converting regexp.exec(str) result to JSON

Expand Messages
  • Jim Fricker
    What would be the recommended way of converting the result of an regexp.exec(str) method call to JSON? The result is an array but with two extra properties;
    Message 1 of 2 , Nov 5, 2007
    • 0 Attachment
      What would be the recommended way of converting the result of an
      regexp.exec(str) method call to JSON? The result is an array but with
      two extra properties; index and input.

      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Glob
      al_Objects:RegExp:exec

      If I use stringify then I just get the array elements and the index
      and input properties are missing. Is there a way of tricking the
      stringify function into treating this as an object rather than an
      array so I can get all the elements/properties/values?
    • Douglas Crockford
      ... myObject = {index: myArray.index, input: myArray.input, array: myArray}; jsontext = JSON.stringify(myObject);
      Message 2 of 2 , Nov 6, 2007
      • 0 Attachment
        --- In json@yahoogroups.com, "Jim Fricker" <yahoo@...> wrote:
        >
        > What would be the recommended way of converting the result of an
        > regexp.exec(str) method call to JSON? The result is an array but with
        > two extra properties; index and input.
        >
        > If I use stringify then I just get the array elements and the index
        > and input properties are missing. Is there a way of tricking the
        > stringify function into treating this as an object rather than an
        > array so I can get all the elements/properties/values?


        myObject = {index: myArray.index, input: myArray.input, array: myArray};
        jsontext = JSON.stringify(myObject);
      Your message has been successfully submitted and would be delivered to recipients shortly.