I have the string SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10]) and I need to get the elements [A2:A10],[B2:B10],[C2:C10],[D2:D10] in an array, so I used match() in js. The code snippet is
var formula = "SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10])";
var reg = /\[(a-zA-Z0-9)+\]/;
matches = formula.match(reg);
But I am not getting the match. I hope the regular expression is wrong. I am very poor in building regular expression. What will be the correct regular expression?
Try it like this:
Output:
Your regex was in the right direction, but didn’t include the colon and captured individual characters. The
\wescape sequence I used is a shortcut for a word character ([a-zA-Z0-9_]), makes it more readable. Thegflag is necessary to get all matches instead of just the first one.