使用javascript Regex解析.gitmodules文件以获取分支,路径,URL

时间:2019-03-12 06:43:21

标签: javascript regex github

我想使用javascript Regex解析.gitmodules文件以获取分支,路径和url的详细信息。

.gitmodules

 [submodule "PATH"]
     path = <PATH>
     url = <URL> 
 [submodule "PATH"]
     path = <PATH>
     url = <URL>
     branch = <BRANCH>

1 个答案:

答案 0 :(得分:0)

这是代码,我已经解决了

    parseGitModules: function (data, callback) {
        var gitModules = data.split("["),
            subModulesCollections = [];

        _.each(gitModules, function (submodules) {
            if (/^submodule*/.test(submodules)) {
                var submodule = submodules.split("\n\t"),
                    moduleDetails = {};

                _.each(submodule, function (sub) {
                    var parseSubmoduleDetails = [],

                    sub = sub.replace(/\n/g,'').replace(/\s/g,'').replace(/\"/g,'=');
                    parseSubmoduleDetails = sub.split("=");

                    switch(parseSubmoduleDetails[0]) {
                        case "submodule":
                            moduleDetails.submodule = parseSubmoduleDetails[1];
                            break;
                        case "path":
                            moduleDetails.path = parseSubmoduleDetails[1];
                            break;
                        case "branch":
                            moduleDetails.branch = parseSubmoduleDetails[1];
                            break;
                        case "url":
                            moduleDetails.url = parseSubmoduleDetails[1];
                            break;
                    };
                });

                subModulesCollections.push(moduleDetails);
            }
        });

        callback(null, subModulesCollections);
    }