Right now, I have the user inputting a chemical formula, say Cu(NO3)2, and splitting the elements and numbers into an array. This code works for all formulas except for those with parentheses. (which actually came from a user-submitted thread on here.)
var userArray=userIn.replace(/\d+/g, '~$&').split(/(?=[A-Z])|~/);
The reason I used replace then split was to ensure that if someone entered H12, it would come out as H , 12 ... rather than H , 1 , 2.
I'm getting Cu( , N , O , 3) , 2 ... when in fact I want... Cu , ( , N , O , 3 , ) , 2
What about using match instead with the g flag:
You need to add the parentheses into your replace pattern, like so:
var userArray=userIn.replace(/\d+|\(|\)/g, '~$&').split(/(?=[A-Z])|~/);