反斜杠 - 正则表达式 - Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10769964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backslashes - Regular Expression - Javascript
提问by Leo Selig
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)
我想构建一个 JS 函数,将参数列表连接到有效路径(因为我无法确定路径的一部分是否带有斜杠)
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.comBut the function does not work properly (RegEx does not match). Furthermore, Chrome states
使用的正则表达式匹配了http://regexpal.com上的所有开始和结束斜杠和反斜杠, 但该功能无法正常工作(正则表达式不匹配)。此外,Chrome 声明
SyntaxError: Invalid regular expression: /()$|^()/: Unterminated group
语法错误:无效的正则表达式:/()$|^()/:未终止的组
when I just use the RegEx
当我只使用 RegEx
(\)$|^(\)
However, using the RegEx
但是,使用正则表达式
(\)$|^(\)
works fine.
工作正常。
Is it too late or did I missed something special?
是不是太晚了,还是我错过了一些特别的东西?
Thanks in advance!
提前致谢!
Leo
狮子座
回答by Mark Reed
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.
您应该使用正则表达式文本(/.../
),而不是一个字符串('...'
或"..."
在调用)replace
。字符串对反斜杠有自己的解释,在正则表达式构造函数破解之前就开始使用,因此您需要额外的引用级别。
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 character classes: [xy]
. In which case you get this:
附带说明一下,当您的替代品是单个字符时,(x|y)
就太过分了;你可以只使用字符类:[xy]
. 在这种情况下,你会得到这个:
path += arguments[i].replace(/[\\/]$|^[\\/]/, "") + "/";
path += arguments[i].replace("[\\/]$|^[\\/]", "") + "/";
回答by agent-j
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.
试试这个……使用 [字符类] 会更容易一些。将单个\
与您需要的 javascript 字符串匹配\\\\
,这可能是正在发生的事情。
new Regexp('^[\\\\/]|[\\\\/]$')
new Regexp('^[\\\\/]|[\\\\/]$')
You can also try the /^[\\\/]|[\\\/]$/g
notation.
您也可以尝试/^[\\\/]|[\\\/]$/g
符号。
s = 'c:\folder\'
console.log(s.replace(/^[\\/]|[\\/]$/g, ''))