I wanted to build a JS function concatting a list of arguments to a valid path (since I could not be sure whether a part of the path is given with or without slashes)
This is the function:
concatPath = function() {
var path = "";
for(var i = 0; i < arguments.length; i++) {
path += arguments[i].replace("(\\|/)$|^(\\|/)", "") + "/";
}
return path;
}
The used RegEx matched all beginning and ending slashes and backslashes on http://regexpal.com But the function does not work properly (RegEx does not match). Furthermore, Chrome states
SyntaxError: Invalid regular expression: /()$|^()/: Unterminated group
when I just use the RegEx
(\\)$|^(\\)
However, using the RegEx
(\\)$|^(\\)
works fine.
Is it too late or did I missed something special?
Thanks in advance!
Leo
\ The backslash suppresses the special meaning of the character it precedes, and turns it into an ordinary character. To insert a backslash into your regular expression pattern, use a double backslash ('\\').
In JavaScript, the backslash has special meaning both in string literals and in regular expressions. If you want an actual backslash in the string or regex, you have to write two: \\ .
The backslash ( \ ) is an escape character in Javascript (along with a lot of other C-like languages). This means that when Javascript encounters a backslash, it tries to escape the following character. For instance, \n is a newline character (rather than a backslash followed by the letter n).
The forward slash character is used to denote the boundaries of the regular expression: ? The backslash character ( \ ) is the escaping character. It can be used to denote an escaped character, a string, literal, or one of the set of supported special characters.
Try this... it's a little easier to follow with the [character classes]. to match a single \
with a javascript string you need \\\\
, which may be what's going on.
new Regexp('^[\\\\/]|[\\\\/]$')
You can also try the /^[\\\/]|[\\\/]$/g
notation.
s = 'c:\\folder\\'
console.log(s.replace(/^[\\\/]|[\\\/]$/g, ''))
You should use a regular expression literal (/.../
) instead of a string literal ('...'
or "..."
) in the call to replace
. Strings have their own interpretation of backslashes that kicks in before the regular expression constructor gets a crack at it, so you need an extra level of quoting.
Match one backslash, regular expression literal: /\\/
Match one backslash, regular expression in a string: '\\\\'
But in a regex literal, you also have to put backslashes in front of the forward slashes, since forward slashes are the delimiter for the whole thing:
path += arguments[i].replace(/(\\|\/)$|^(\\|\/)/, "") + "/";
Or, if you're married to the use of strings for some reason, this should also work:
path += arguments[i].replace("(\\\\|/)$|^(\\\\|/)", "") + "/";
As a side note, when your alternatives are single characters, (x|y)
is overkillish; you can just use a character class ([xy]
). In which case you get this:
path += arguments[i].replace(/[\\\/]$|^[\\\/]/, "") + "/";
path += arguments[i].replace("[\\\\/]$|^[\\\\/]", "") + "/";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With