Jump to content

Split() perserving the values split with


viviosoft

Recommended Posts

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

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"}]

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"}]

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/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.