6

the string is the name of some input fields. Examples:

a[b][c]

x[y][z][]

I'm trying to produce multidimensional arrays from it.

a.b.c = value

x.y.z = [value] (value is an array because of [])

anyway i have some ideas on how to do that but first I need to split the string into an array that contains all keys and subkeys.

Searching google I've come up with this

var parts = inputName.match(/[^\[\]]+/g);

it gets me [a,b,c], but on the 2nd example I get [x,y,z] instead of [x,y,z, null]

Any way I can also match [] and add it to the list as null ? I'm not that familiar with regular expressions


@AnnaK. Why can't you simply use ids and keys to send your information? Is it a form that is dynamic in its size? Is it because you're developing a framework that needs to be dynamic? Is it because you're using a function multiple places? Can't you serialize it by using the names? We can't give you suggestions because we still don't know why you need to transfer it as an array (which would be serialized anyway), and we still don't know what your form looks like

Because I'm trying to send the form data with Ajax. Also, before I send the data, I need to prepend a few fields that I create with javascript. I cannot serialize it, at least not with jQuery.serializeArray if that's what you meant, because if I have multiple keys like x[y][z][], only the last one will appear

10
  • 2
    What is the XY problem? Commented Nov 25, 2013 at 16:23
  • And what do you think it's my actual problem? The fact that I need build a multidimensional array from form fields? Commented Nov 25, 2013 at 16:25
  • 3
    I don't know what your actual problem is, and neither does anyone else. I don't post an answer simply because I believe there to be a better way than what you're trying to do. Why do you need to build a multidimensional array from form fields? How do you build this array? What does the form look like? There's no need to be passive aggressive. Commented Nov 25, 2013 at 16:28
  • If you don't know, then why do you think it's a xy problem? I need the array so I can submit the form with ajax Commented Nov 25, 2013 at 16:31
  • 1
    It's an XY problem because answering the question above is likely to be ultimately less helpful to you than getting to the root problem. If you tell people why you think you need to split strings by square brackets, then perhaps they can give you an alternative approach that doesn't even need you to do this. Commented Nov 25, 2013 at 16:44

3 Answers 3

8

You can use the following solution:

var parts = inputName.split(/[[\]]{1,2}/);
parts.length--; // the last entry is dummy, need to take it out

console.log(parts); // ["x", "y", "z", ""] 

If you need the null value (because there is processing you have no control over for example) you can use this snippet to update the result (curtesy of @Ties):

parts = parts.map(function(item){ return item === '' ? null : item });
Sign up to request clarification or add additional context in comments.

5 Comments

var parts = inputName.split(/[[\]]{1,2}/).map(function(a){ return (a=="")?null:a; }); will return null instead of ""
@Ties Thanks for the addition, i will improve the answer (though i would expect changing the condition from null to '' would be easier).
yeah i know, thats why i thought it would make a nice comment instead of a new answer ;)
This returns null even on a[b][c] => ['a', 'b', 'c', null]
@hwnd I'm guessing you forgot the second line of the solution. It works well for me.
3

Basic idea I had was to look for a [] and than use map() to replace it with null.

("a[bb][c][]").match(/([^[\]]+|\[\])/g).map( function(val) { return val==="[]" ? null : val; });

Comments

2

Regex: ([^\[\]]|\[\])+

Will match:

a[b][c]       // a, b, c
x[y][z][]     // x, y, z
1[][2][][3][] // 1, [], 2, [], 3, []

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.