我有一个字符串:
users/554983490\/Another+Test+/Question????\/+dhjkfsdf/
我如何编写一个RegExp,它将匹配所有前斜线,而不是反斜杠?
编辑:有没有办法在不使用负面背后的情况下做到这一点?
答案 0 :(得分:2)
您可以使用:
/(?<!\\)\//
我使用/作为分隔符
(?<! <-- Start of the negative lookbehind (means that it should be preceded by the following pattern)
\\ <-- The \ character (escaped)
) <-- End of the negative lookbehind
\/ <-- The / character (escaped)
答案 1 :(得分:2)
如果您的正则表达式支持negative lookbehinds:
/(?<!\\)\//
否则,您需要匹配/
之前的字符:
/(^|[^\\])\//
这匹配字符串的开头(^
)或(|
)除\
([^\\]
)以外的任何内容作为捕获组#1 {{ 1}}。然后它匹配文字()
之后。在/
之前的任何字符都将存储在捕获组/
中,以便在进行替换时将其重新放入....
示例(JavaScript):
$1