Robert C. Barth
Robert C. Barth

Reputation: 23315

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 (e.g. /something/), but skip escaped pairs (e.g. \/something\/) and also skip something like subsequent URL's (e.g. 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:

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 (i.e. no look-behinds).

Upvotes: 0

Views: 132

Answers (3)

anubhava
anubhava

Reputation: 784998

How about using this regex:

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

And use matched group #1 for your match.

Regex Demo

Upvotes: 1

progrenhard
progrenhard

Reputation: 2363

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

Regular expression visualization

Debuggex Demo

Upvotes: 1

Douglas
Douglas

Reputation: 37763

Tricky, since you probably want foo /bar/ foo /bar/ foo to not match / 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.

Upvotes: 0

Related Questions