I have the following data posibilities
fnname()
fnname(value)
fnname(value,valueN)
I need a way to parse it with javascript regex to obtain an array
[fnname]
[fnname,value]
[fnname,value,valueN]
Thanks in advance!
You could try matching rather than splitting,
> var re = /[^,()]+/g;
undefined
> var matches=[];
undefined
> while (match = re.exec(val))
... {
... matches.push(match[0]);
... }
5
> console.log(matches);
[ 'fnname', 'value', 'value2', 'value3', 'value4' ]
OR
> matches = val.match(re);
[ 'fnname',
'value',
'value2',
'value3',
'value4' ]
This should work for you:
var matches = string.split(/[(),]/g).filter(Boolean);
/[(),]/g
is used to split on any of these 3 characters in the character classfilter(Boolean)
is used to discard all empty results from resulting arrayExamples:
'fnname()'.split(/[(),]/g).filter(Boolean);
//=> ["fnname"]
'fnname(value,value2,value3,value4)'.split(/[(),]/g).filter(Boolean);
//=> ["fnname", "value", "value2", "value3", "value4"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With