Regex Replace with Capture not ending with String

Refresh

March 2019

Views

27 time

1

I need some help

My pattern looks like

/°>(http|\/)([^|<]+?)(?!\.gif|\.jpg|\.mp|\.png|\.jpeg)<°/gi

I have 4 examples for what I try to achieve:

°>/w NICK<°     

$1 -> /
$2 -> w NICK

°>http://google.de<°

$1 http $2 ://google.de

°>/w NICK|/w NICK<°

no matches because of the |

°>http://google.de/img.png<°

no matches because of ending with .png

Now im totally clueless.... iI got the regex for the first 3 examples working, but can't get the lookahead for ending with a img extension in c# i got it working but not for javascript

1 answers

2

Use

°>(http|\/)(?![^|<]*\.(?:gif|jpe?g|mp|png)<)([^|<]+)<°

See the regex demo

Details:

  • °> - a literal substring
  • (http|\/) - Group 1: http or /
  • (?![^|<]*\.(?:gif|jpe?g|mp|png)<) - a negative lookahead that fails the match if, immediately after http or /, there are 0+ chars other than | and < followed with some extensions specified in the non-capturing group that are followed with <
  • ([^|<]+) - Group 2 matching 1 or more chars other than | and <
  • - a literal substring.