Reputation: 69
How to write the regular expression for the below string using Javascript.
[Account].&[1]+[Account].&[2]+[Account].&[3]+[Account].&[4]
T need the following output format
1,2,3,4
emoving all the strings and special characters.
Upvotes: 0
Views: 596
Reputation: 17831
/\[(\d+)\]/g
That will return you the array containing all numbers in the string, which you then can join using ,
as delimiter.
var string = "[Account].&[1]+[Account].&[2]+[Account].&[3]+[Account].&[4]";
var numbers = string.match(/\[(\d+)\]/gi);
alert(numbers.join(','));
Upvotes: 3
Reputation: 4775
This will give you an array of the extracted numbers:
var ar1 ='[Account5].&[1]+[Account6].&[2]+[Account7].&[3]'.match(/\[\d+\]/g);
And to assert you extract only numbers between brackets:
var ar2 = ar1.map(function (elt) { return elt.replace(/\[(\d+)\]/, "$1"); });
Upvotes: 0