简体   繁体   English

正则表达式,以匹配成对的正斜杠,但不能转义,也不能在后续的http://之间

[英]Regular Expression to match pairs of forward slashes, but not escaped or between subsequent http://

I'm trying to build a regular expression that can match paired forward slashes (eg /something/ ), but skip escaped pairs (eg \\/something\\/ ) and also skip something like subsequent URL's (eg http://something.com and stuff http://somethingelse.org ). 我正在尝试构建一个可以匹配成对的正斜杠(例如/something/ )的正则表达式,但是跳过转义的对(例如\\/something\\/ ),并且还跳过类似后续URL的内容(例如http://something.com and stuff http://somethingelse.org )。

So, in the following example, only the text "jumped over" would be matched, nothing else would be a match: 因此,在下面的示例中,将仅匹配文本“ jumped over”,没有其他匹配项:

The quick brown fox /jumped over/ the lazy dogs. He was looking for a 
\/website\/ to help him find ways around the dogs because he was sick of 
\/jumping\/ over them. Unfortunately, both http://routesaroundlazydogs.com/ and 
https://maps.lazydogs.com/stuff/things/findmap.aspx were both down on the day he 
was looking.

The regex has to work in Javascript (ie no look-behinds). 正则表达式必须使用Javascript(即无后顾之忧)。

How about using this regex: 如何使用此正则表达式:

\\\/.*?\\\/|\/\/\S*|\/(.*?[^\\])\/

And use matched group #1 for your match. 并使用匹配的#1组进行匹配。

Regex Demo 正则表达式演示

  (^|\s+)/([A-Z0-9a-z ]+)/\s+

正则表达式可视化

Debuggex Demo Debuggex演示

Tricky, since you probably want foo /bar/ foo /bar/ foo to not match / foo / . 棘手的,因为您可能希望foo /bar/ foo /bar/ foo不匹配/ foo /

I'd suggest finding a url regex, then doing input.replace(urlRegex, "") , then writing a simple parser to read in the / pairs instead of a regex. 我建议找到一个url正则表达式,然后执行input.replace(urlRegex, "") ,然后编写一个简单的解析器以读取/对而不是正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM