次の Javascript を使用して Windows フォルダー パスをクリーンアップしようとしています。
function StandardizeFolderPath(inFolderPath) {
var outFolderPath = inFolderPath.replace(/^\s+|\s+$/g, "");
outFolderPath = outFolderPath.replace(/\\\s*/g, "\\");
outFolderPath = outFolderPath.replace(/\s*\\/g, "\\");
outFolderPath = outFolderPath.replace(/\\{2,}/, "\\");
alert("^" + inFolderPath + "$ " + "^" + outFolderPath + "$");
return outFolderPath;
}
function Test_StandardizeFolderPath() {
StandardizeFolderPath("D:\\hel xo \\");
StandardizeFolderPath("D:\\hello \\ ");
StandardizeFolderPath("D:\\hello \\ \\");
StandardizeFolderPath("D:\\hello \\ mike \\");
StandardizeFolderPath(" D:\\hello \\ jack \\");
StandardizeFolderPath(" D:\\hello Multiple Slashes \\\\");
}
各置換は特定の部分を行います:
- 前後のスペースを削除
"\ "
いずれかを置き換えます"\"
- 置き換えます
" \"
- 複数出現する
"\"
を 1 つに置き換えます。
それは仕事を終わらせますが、もっと良い方法があるかどうか知りたいです(説明付き)