2

How to find the url which comes before the first backbone.js token in the given text. I tried all possibilities but not able to do so.

Ref: https://regex101.com/r/rJ1oA8/4

Text: dfjSKsfkfkSFKfdvsfdvsdv https://text.com from https://test2.com sdjgdsdh https://backbone.js from jfsjfskf https://backbone.js jsvdywfiwqkbfs sfgsaifiwf sfasfk https://fggfgf.com

Expected output: https://test2.com

Text can have any number of urls

kalki
  • 465
  • 7
  • 14

2 Answers2

1

I can suggest this regex:

\bhttps?:\/\/\S*(?=(?:(?!http)[\s\S])*https?:\/\/\S*backbone\.com)

The (?:(?!http)[\s\S])* is a tempered greedy token that ensures the smallest possible window between a URL and another URL with backbone.com.

In JS, to match any character, even a newline, you need to use [\s\S] character class, or a JS-specific [^]. For portabililty reasons, I prefer [\s\S].

See demo

For a better URL regex, you can see this SO post or see my previous answer with Diego Perini's URL regex.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • But it is not working if the text contains new line charterers inbetween, ref: https://regex101.com/r/rJ1oA8/7 – kalki Sep 10 '15 at 09:01
0

You might want to try this

https?\:\/\/(?!backbone)\S+\.com(?=(?:(?!http)[^])*https?\:\/\/backbone.js)

Demo here(https://regex101.com/r/xP3rR5/1)

Neil Villareal
  • 627
  • 9
  • 14