Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  124] [ 5]  / answers: 1 / hits: 23176  / 12 Years ago, wed, march 28, 2012, 12:00:00

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?


More From » regex

 Answers
39

Try it like this:



var formula = 'SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10])';
var reg = /[w+:w+]/g;
matches = formula.match(reg);


Output:



[[A2:A10], [B2:B10], [C2:C10], [D2:D10]]


Your regex was in the right direction, but didn't include the colon and captured individual characters. The w escape sequence I used is a shortcut for a word character ([a-zA-Z0-9_]), makes it more readable. The g flag is necessary to get all matches instead of just the first one.


[#86570] Monday, March 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryleymarkelb

Total Points: 554
Total Questions: 106
Total Answers: 95

Location: Norway
Member since Mon, May 23, 2022
2 Years ago
;