Sagar Chaudhary
Sagar Chaudhary

Reputation: 1403

Parser not working properly

if i have,

str = "[App Version]+4-[Number 1 Q1-P3]"

i want an array like :

arr[0] = App Version  
arr[1] = Number 1 Q1-P3

My current parser:

 var arr =str.replace(/[+/*//[]/g,'').split("]").slice(0, -1);  
    arr = arr.map(function(item){
       return item.replace(/^-/, '')
    });

It gives result:

arr[0] = App Version  
arr[1] = 4-Number 1 Q1-P3

I only want the name between [ ] in an array.
Can anyone provide a regex or something?

Upvotes: 1

Views: 148

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370699

Try this:

const input = "[App Version]+4-[Number 1 Q1-P3]";
const matches = input.match(/[^\[]+(?=\])/g);
console.log(matches);

Upvotes: 2

Related Questions