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. Quote 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. Quote 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 (edited) 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"}] Edited March 15, 2013 by viviosoft Quote 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. Quote 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 (edited) 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"}] Edited March 15, 2013 by viviosoft Quote Link to comment https://forums.phpfreaks.com/topic/275676-split-perserving-the-values-split-with/#findComment-1418739 Share on other sites More sharing options...
Solution viviosoft Posted March 15, 2013 Author Solution Share Posted March 15, 2013 (edited) 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/ Edited March 15, 2013 by viviosoft Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.