正则表达式匹配正斜杠对,但不转义或在后续http://之间

时间:2014-07-24 22:18:07

标签: javascript regex

我尝试构建一个可以匹配成对正斜杠的正则表达式(例如/something/),但是跳过转义对(例如\/something\/)并且还跳过后续URL&#39之类的内容; s(例如http://something.com and stuff http://somethingelse.org)。

因此,在下面的例子中,只有文字"跳过"将匹配,没有别的匹配:

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.

正则表达式必须使用Javascript(即没有后视)。

3 个答案:

答案 0 :(得分:1)

如何使用此正则表达式:

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

并使用匹配的组#1进行比赛。

Regex Demo

答案 1 :(得分:1)

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

Regular expression visualization

Debuggex Demo

答案 2 :(得分:0)

棘手,因为您可能希望foo /bar/ foo /bar/ foo/ foo /不匹配。

我建议找一个url正则表达式,然后执行input.replace(urlRegex, ""),然后编写一个简单的解析器来读取/对而不是正则表达式。