Jump to content

Split() perserving the values split with


Go to solution Solved by 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"}]

Edited by viviosoft

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 by viviosoft
  • Solution

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 by viviosoft
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.