If I have a string in javascript like =sum(A1,B2)
or an example variation like = sum( C1, ZX2)
. The combinations of cells could be anything. I need a function to parse out the two parameters. In Javascript using regex.
Asked
Active
Viewed 971 times
0

Grzegorz Oledzki
- 23,614
- 16
- 68
- 106

user439526
- 371
- 2
- 5
- 12
-
Can your formulae include absolute references (eg $A3, A$3 or $A$3) or references to cells on other worksheets (eg 'Sheet 1'!C5) or even other workbooks ([C:\Excel\Workbook2.xls]Sheet1!A1)? Or might they include references in R1C1 format? – Mark Baker Feb 23 '11 at 08:40
-
Possible duplicate [LINK](http://stackoverflow.com/questions/1897906/is-this-the-regex-for-matching-any-cell-reference-in-an-excel-formula) – chris neilsen Feb 23 '11 at 09:02
2 Answers
1
This regexp matches those strings:
\s?([A-Z]+[0-9]+).*?([A-Z]+[0-9]+)
In JavaScript you'd use this:
string.match('([A-Z]+[0-9]+).*?([A-Z]+[0-9]+)/')
It returns an array of matches, in this case it would return ["A1,A2"], you'd still need to explode the string at ','

deiga
- 1,587
- 1
- 13
- 32
-
How can this be used in javascript to get for example A1 and A2 from a formula string =sum(A1,A2) ? – user439526 Feb 23 '11 at 08:44
-
`string.match('([A-Z]+[0-9]+).*?([A-Z]+[0-9]+)/')` Returns an array of matches, in this case it would return ["A1,A2"], you'd still need to explode the string at ','. – deiga Feb 24 '11 at 06:24
1
/(\[A-Z]+[0-9]+)\s*,\s*(\[A-Z]+[0-9]+)/
should work on your examples. Will you also have to handle ranges like A12:C34
?

Tim Pietzcker
- 328,213
- 58
- 503
- 561