最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Split string on [] brackets - Stack Overflow

programmeradmin4浏览0评论

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 e 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

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 e 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

Share Improve this question edited Nov 25, 2013 at 17:04 Anna K. asked Nov 25, 2013 at 16:21 Anna K.Anna K. 1,9956 gold badges27 silver badges40 bronze badges 10
  • 2 What is the XY problem? – h2ooooooo 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? – Anna K. 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. – h2ooooooo 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 – Anna K. 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. – cmbuckley Commented Nov 25, 2013 at 16:44
 |  Show 5 more ments

3 Answers 3

Reset to default 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 });

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; });

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

Will match:

a[b][c]       // a, b, c
x[y][z][]     // x, y, z
1[][2][][3][] // 1, [], 2, [], 3, []
发布评论

评论列表(0)

  1. 暂无评论