For example, here is a string representing an expression:
var str = 'total = sum(price * qty) * 1.09875';
I want to extract variables (i.e., ‘total’, ‘price’ and ‘qty’ but not ‘sum’ since ‘sum’ is a function name) from this expression. What is the regexp pattern in javascript? Variable name consists of letters, digits, or the underscore, beginning with letters or the underscore.
Here,
[a-z_]matches 1 letter or 1 underscore,\w*matches 0 or more letters, digits or underscore (\wmeans[a-zA-Z0-9_])(?!…)is a negative lookahead. The match will fail if the stuff inside is matched.\w*\s*\(matches some letters, followed by some spaces, and then an(. This allows function names to be rejected.