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

Anna K.
  • 1,887
  • 6
  • 26
  • 38
  • 2
    [What is the XY problem?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – h2ooooooo Nov 25 '13 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? – Anna K. Nov 25 '13 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. – h2ooooooo Nov 25 '13 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 – Anna K. Nov 25 '13 at 16:31
  • Read about what an XY problem is — perhaps there is an easier way to solve your actual underlying problem (X) instead of the solution you have above (Y). – cmbuckley Nov 25 '13 at 16:34
  • Yes, I read it just after h2o posted the comment, and I don't see how does this qualify as XY problem. If you think there are better ways of accomplishing what I want, please be my guest, I'm open to suggestions. But telling me that you think I'm trying to solve a problem that doesn't exist without telling me what problem do you think I'm trying to solve, doesn't really make much sense – Anna K. Nov 25 '13 at 16:37
  • @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. – h2ooooooo Nov 25 '13 at 16:39
  • This should get you started: http://stackoverflow.com/q/6491463/218196. – Felix Kling Nov 25 '13 at 16:40
  • You should use `split` instead of `match`. Check @Tibos answser. – Stephan Nov 25 '13 at 16:40
  • 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. – cmbuckley Nov 25 '13 at 16:44

3 Answers3

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 });
Tibos
  • 27,507
  • 4
  • 50
  • 64
  • 1
    `var parts = inputName.split(/[[\]]{1,2}/).map(function(a){ return (a=="")?null:a; });` will return null instead of "" – Ties Nov 25 '13 at 16:40
  • @Ties Thanks for the addition, i will improve the answer (though i would expect changing the condition from null to '' would be easier). – Tibos Nov 25 '13 at 16:42
  • yeah i know, thats why i thought it would make a nice comment instead of a new answer ;) – Ties Nov 25 '13 at 16:50
  • This returns `null` even on `a[b][c]` => `['a', 'b', 'c', null]` – hwnd Nov 25 '13 at 17:05
  • @hwnd I'm guessing you forgot the second line of the solution. It works well for me. – Tibos Nov 25 '13 at 17:07
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; });
epascarello
  • 204,599
  • 20
  • 195
  • 236
2

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

Will match:

a[b][c]       // a, b, c
x[y][z][]     // x, y, z
1[][2][][3][] // 1, [], 2, [], 3, []
givanse
  • 14,503
  • 8
  • 51
  • 75