I have an expression string from Angular [ngClass] directive containing multiple expressions with their corresponding key separated by a comma.
'background_orange':row.columnname.split(',')[4] == 1,'red:color':test==='testname','yellow:color':test==='testname'
When I try to split the above expression string into an array of individual expression with their using the .split(',') function it is splitting the in between the individual expressions.
Code:
var temp = "'background_orange':row.columnname.split(',')[4] == 1,'red:color':test==='testname','yellow:color':test==='testname'";
var result = temp.split(',');
console.log(result);
Output:
["'background_orange':row.columnname.split('", "')[4] == 1", "'red:color':test==='testname'", "'yellow:color':test==='testname'"]
Expected Result:
["'background_orange':row.columnname.split(',')[4] == 1", "'red:color':test==='testname'", "'yellow:color':test==='testname'"]
I tried to build regex to extract the individual expression but able to only extract the expression keys with the below regex
Regex:
'\b(.+?)':
What would be a regex to extract the above mentioned expected pattern
The following regex splits your string at ,
but ignores all ,
inside strings between "
or '
:
var temp = "'background_orange':row.columnname.split(',')[4] == 1,'red:color':test==='testname','yellow:color':test==='testname'";
var r = /(('[^']*')|("[^"]*")|[^,])+/g
var result = temp.match(r)
console.log(result);
Each match is a non-empty sequence of:
('[^']*')
letters not being '
between '
("[^"]*")
same as above with "
[^,]
a char not being ,