Javascript: How to replace [ ] and "?

arrays, javascript, php, regex, split

Solution

If you transfer a string value like `"[5,4,7,2,1]"` from PHP to Javascript, you should rather `JSON decode` it into a native Javascript object/array.

var receivedData = "[5,4,7,2,1]";
    receivedData = JSON.parse( receivedData );

From that point on, you may just access `receivedData` like a normal Array

console.log( receivedData[ 2 ] );  // 7

Problem

I am sending a Array from PHP encoded it to JSON and send it to a JavaScript value. Now I wanted to split the Array on comma's `,`. This worked fine, but when calling the very first and last string inside the array I can see a `[`, `]` and `"`. I can get the `"` gone, but the `[` and `]` seems to be a problem. my code now: ``` data_array[i].replace(/["[]]/g,""); ```

Original source