Pranav Shah
Pranav Shah

Reputation: 3323

Is there a way to improve this Javascript Regex replace

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)

Upvotes: 0

Views: 84

Answers (2)

Denys Séguret
Denys Séguret

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

6502
6502

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

Related Questions