1

I am trying to add some rules to Imagus Firefox Extension. I want to capture image parameter from Google Image Search and if it contains the string th_ remove it and redirect. Otherwise just redirect.

This is my RegEx:

/^(?:(?:images|encrypted)\.)?google\.[^/]+/(?:imgres\?(?:[^&]+&)*?imgurl=)(.*)(?:th_)(.*)&imgrefurl=.*/gm

It works fine for URL's which contain string th_ but for other links it breaks.

Here's the link to my work https://regexr.com/3omf5 Have a look and help.

PS: Please note there are two links in the example.

5
  • Remove (?:th_) in your regex? Commented Apr 24, 2018 at 12:55
  • Try with optional quantifier(?). It checks for 0 or 1 occurrence. (?:th_)? Commented Apr 24, 2018 at 13:15
  • Tried, but it doesn't remove the 'th_' in result URL. Commented Apr 24, 2018 at 13:22
  • 1
    Can you please provide a simplified example with input and result? Commented Apr 24, 2018 at 13:40
  • @zᴉɹɥƆ I've attached a simplified example to the question. Commented Apr 24, 2018 at 14:01

3 Answers 3

2

I found the answer after a fight. And the regex works fine in the Extension.

Ans:

^(?:(?:images|encrypted)\.)?google\.[^/]+/(?:imgres\?(?:[^&]+&)*?imgurl=)(.*)(%2Fimages(?:[\d]{1,9})?%2F)(th_)?(.*)&imgrefurl=.*

Here is th link with answer:
https://regexr.com/3omfh

Sign up to request clarification or add additional context in comments.

1 Comment

Well, fine, I was still looking :). You can accept your own answer als well. You still need to escape a slash here: ...google\.[^/]+-->\<--/...
0

Add a * after (?:th_), like:

^(?:(?:images|encrypted)\.)?google\.[^/]+/(?:imgres\?(?:[^&]+&)*?imgurl=)(.*)(?:th_)*(.*)&imgrefurl=.*

1 Comment

Then it doesn't remove the th_ from the first link.
0
^(?:(?:(?:images|encrypted)\.)?google\.[^/]+/(?:imgres\?(?:[^&]+&)*?imgurl=)(.*)(?:th_)(.*)&imgrefurl=.*)|(.+)

Matches your urls with th_ and replaces it or takes the whole url with the additional |(.+) (+ ^(?: ... ) around your regex). You need to replace it with $1$2$3 then

1 Comment

Then it breaks the link2 which doesn't contain 'th_' then I tried using (?) after (?:th_)? then it doesn't remove the 'th_' from the link 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.