0

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:

  1. Remove spaces from fron and back
  2. Replace any "\ " with "\"
  3. Replace any " \"
  4. Replace multiple occurrences of "\" with a single.

It gets the job done, but I want to know if there is a better way (with explanation)

7
  • You don't have any test case for the 4. Commented Sep 18, 2013 at 17:20
  • @dystroy 4th gets tested with this statement "D:\\hello \\ \\" Commented Sep 18, 2013 at 17:26
  • Partially only. There could be no space in between. But you know what can occur in the real cases, I was just pointing this omission relative to your rules. Commented Sep 18, 2013 at 17:29
  • @dystroy Thanks for pointing that out. Does the code that you provided below also solve that. Commented Sep 18, 2013 at 17:35
  • Y U NO .trim() for #1 replace? Commented Sep 18, 2013 at 17:44

2 Answers 2

2

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 :

Sign up to request clarification or add additional context in comments.

11 Comments

Well... not the first one that I've left untouched.
You're missing the {2,} from the fourth one.
@PeterBoughton No, I don't. There's the + for that. It also takes care of "\\\\" or "\\ \\".
@dystroy Thanks that does help. Could you please add a little explanation to your answer so that I can understand better. Significance of "/" at the beginning and "/g" at the end. Also how the "()+" works.
@PranavShah I detailed and added some links you should read to get a better understanding
|
0

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.

4 Comments

I don't think it works for now. Try "\\ a". It can be fixed of course but I find this hard to maintain and check.
@dystroy: true, I don't see how to do everything with just one pass without look-behind zero-width assertions that are not supported everywhere. Added a second pass.
In fact I think that those assertions are supported in no browser.
@dystroy: while dining I got the idea that using a capturing group could make it... seems it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.