簡體   English   中英

正則表達式,以匹配成對的正斜杠,但不能轉義,也不能在后續的http://之間

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

我正在嘗試構建一個可以匹配成對的正斜杠(例如/something/ )的正則表達式,但是跳過轉義的對(例如\\/something\\/ ),並且還跳過類似后續URL的內容(例如http://something.com and stuff http://somethingelse.org )。

因此,在下面的示例中,將僅匹配文本“ 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.

正則表達式必須使用Javascript(即無后顧之憂)。

如何使用此正則表達式:

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

並使用匹配的#1組進行匹配。

正則表達式演示

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

正則表達式可視化

Debuggex演示

棘手的,因為您可能希望foo /bar/ foo /bar/ foo不匹配/ foo /

我建議找到一個url正則表達式,然后執行input.replace(urlRegex, "") ,然后編寫一個簡單的解析器以讀取/對而不是正則表達式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM