Reputation: 3323
I am trying to clean-up a windows folder path using the following Javascript.
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 \\\\");
}
Each replace does specific parts:
"\ "
with "\"
" \"
"\"
with a single.It gets the job done, but I want to know if there is a better way (with explanation)
Upvotes: 0
Views: 84
Reputation: 382150
You could merge three of your replacements :
function StandardizeFolderPath(inFolderPath) {
return inFolderPath.replace(/^\s+|\s+$/g, "").replace(/(\s*\\\s*)+/g, "\\");
}
Here's what /(\s*\\\s*)+/g
means :
NODE EXPLANATION
--------------------------------------------------------------------------------
/ start of regex literal
--------------------------------------------------------------------------------
( group \1:
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\\ '\'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
)+ end of \1 . The + makes it work for one or
more occurences
--------------------------------------------------------------------------------
/ end of regex literal
--------------------------------------------------------------------------------
g executes more than once
References :
Upvotes: 2
Reputation: 114481
A single regular expression
s.replace(/ *(\\)(\\? *)+|^ *| *$/g, "$1")
seems to do the trick.
The idea is that of a block made up with spaces followed by a backslash followed by a sequence of other backslashes or spaces you want to keep only the backslash.
The other cases are for removing initial and ending spaces.
Upvotes: 0