viviosoft Posted March 15, 2013 Share Posted March 15, 2013 Okay - I'm trying to split the following string while perserving the values I used to split the string. I've tried searching on this and can't seem to figure out how to accomplish what I'm after using examples I've found. Here's what I have so far: var string = '[{"require":true,"minLengthInput":"6"},{"require":true,"emailAddress":"email"}]'; console.log(string.split(/},{(?=},{/)); So I want to perserve the },{ in the objects that come back. The above doesn't work at all but the output I want is: var[0] = [{"require":true,"minLengthInput":"6"}, var[1] = {"require":true,"emailAddress":"email"}] Thanks for any help you can provide me. Link to comment https://forums.phpfreaks.com/topic/275676-split-perserving-the-values-split-with/ Share on other sites More sharing options...
requinix Posted March 15, 2013 Share Posted March 15, 2013 Sounds like you should be parsing the JSON instead. Link to comment https://forums.phpfreaks.com/topic/275676-split-perserving-the-values-split-with/#findComment-1418733 Share on other sites More sharing options...
viviosoft Posted March 15, 2013 Author Share Posted March 15, 2013 Haha, no, I know I can convert the string into an object but that's not want I want to do. I want to do what I asked above. I need to perserve the string as-is. var[0] = [{"require":true,"minLengthInput":"6"},var[1] = {"require":true,"emailAddress":"email"}] Link to comment https://forums.phpfreaks.com/topic/275676-split-perserving-the-values-split-with/#findComment-1418737 Share on other sites More sharing options...
Jessica Posted March 15, 2013 Share Posted March 15, 2013 It already IS JSON. Treat it as such. Link to comment https://forums.phpfreaks.com/topic/275676-split-perserving-the-values-split-with/#findComment-1418738 Share on other sites More sharing options...
viviosoft Posted March 15, 2013 Author Share Posted March 15, 2013 I know it's JSON. If I convert the string into an JSON array (object) (which is NOT what I want), then it doesn't perserve the output in the format I want. In stead of telling me what I already know, can someone please just provide help with my original question? So, with that out of the way, this: http://jsfiddle.net/Ez3b3/51/ does close to what I'm after. However, it's returning the }, on the second line and it needs to be on the first line as indicated here which is the output I'm after: [0] = [{"require":true,"minLengthInput":"6"},[1] = {"require":true,"emailAddress":"email"}] Link to comment https://forums.phpfreaks.com/topic/275676-split-perserving-the-values-split-with/#findComment-1418739 Share on other sites More sharing options...
viviosoft Posted March 15, 2013 Author Share Posted March 15, 2013 Thanks anyway, I figured it out. Someone else needing this solution can find it here: var string = '[{"require":true,"minLengthInput":"6"},{"require":true,"emailAddress":"email"}]'; console.log(string.split(/(?={)/)); jsFiddle: http://jsfiddle.net/Ez3b3/72/ Link to comment https://forums.phpfreaks.com/topic/275676-split-perserving-the-values-split-with/#findComment-1418746 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.