Regex to grab strings between square brackets

前端 未结 6 549
[愿得一人]
[愿得一人] 2020-12-03 11:11

I have the following string: pass[1][2011-08-21][total_passes]

How would I extract the items between the square brackets into an array? I tried

6条回答
  •  遥遥无期
    2020-12-03 11:48

    var s = 'pass[1][2011-08-21][total_passes]';
    
    r = s.match(/\[([^\]]*)\]/g);
    
    r ; //# =>  [ '[1]', '[2011-08-21]', '[total_passes]' ]
    
    example proving the edge case of unbalanced [];
    
    var s = 'pass[1]]][2011-08-21][total_passes]';
    
    r = s.match(/\[([^\]]*)\]/g);
    
    r; //# =>  [ '[1]', '[2011-08-21]', '[total_passes]' ]
    

提交回复
热议问题