javascript - regex js parse string of functions -
i'm looking create regex expression parse string of functions, typically looking this:
" b ( ) c d ( 1, 2 ) text(' asdf sg sf sd sdf ') "
into array spaces removed except spaces within either single or double quotes. ie, this:
['a','b()','c','d(1,2)',"text(' asdf sg sf sd sdf ')"]
i'm sure type of problem has been around since birth of computing surprised can't find neat regex solution problem on web!
any appreciated!
just mean--a sequence of letters (\w+
), maybe (?
) followed white space (\s*
) , parenthesized (\(.*?\)
:
var str = " b ( ) c d ( 1, 2 ) text(' asdf sg sf sd sdf ') "; var re = /\w+(\s*\(.*?\))?/g; // ^^^ identifier // ^^^^^^^ arglist // remove spaces in each result, except spaces in "string literals" 'a b'. function squeeze(str) { return str.replace(/'.*?'| /g, match => match === ' ' ? '' : match); } console.log(str.match(re).map(squeeze));
this cannot handle nested function calls. close impossible regexp.
Comments
Post a Comment