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("[\\/]$|^[\\/]", "") + "/";
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…