写一个命令行字符的解析函数。
例:parse('-name lily -age 25 -school "chengdu university"');
返回的是[-name lily,-age 25, -school "chengdu university"]
正则
/(?:-([A-Za-z]+)\s+([A-Za-z0-9]+|\".*?\")\s*)/g
推荐一个网址测试正则 http://www.regexr.com/
function parse(str) {
var ans = {};
var parsePatt = /(?:-([A-Za-z]+)\s+([A-Za-z0-9]+|\".*?\")\s*)/g;
var result;
do {
result = parsePatt.exec(str);
if(result) {
ans[result[1]] = result[2];
}
} while (result != null)
return ans;
}
parse('-name lily -age 25 -school "chengdu university"');