0

I have two strings

http://dfdkdlkdkldkldkldl.jpg (it is image src which is staring with http and ending with image)

http://fflffllkfl

now i want to replace http:// with sometext only on those url which having image

what i tried (http(s?):)|([/|.|\w|\s])*.(:jpg|gif|png)

it replacing http of both string.

Any body can help

Thanks

1
  • A URL that ends in an image file-type should start with https://, is that what you're asking? And where are these strings coming from? The src of images, the href of links, in the plain text? You've got >1.1k rep as I type; you know you need to form your question better than this. Commented Nov 25, 2014 at 12:16

2 Answers 2

1

Here is a valid regex:

(https?:)\/\/.*(jpg|gif|png)

That will only match the "image" url. You can play around with it online here: http://regex101.com/

Edit Basically, your Regex was not only invalid, but too convoluted. You had a sub-group for the "s" on "https", which wasn't needed according to the problem you proposed. Also, you had the OR operand trying to separate the http part and the rest of the url, which made no sense..., lastly, you were grouping the text between ":" and the dot ".", which again, according to your problem description it wasn't needed.

Hope that helps

Edit 2

Ok, so I don't know how exactly the replacement is being done, you're not using your code, you're using a page for that, but here is how you should be doing it:

"http://dfdkdlkdkldkldkldl.jpg".replace(/(https?:)(\/\/.*)(jpg|gif|png)/, "lalala$2$3")

Note that the RegEx changed to: (https?:)(\/\/.*)(jpg|gif|png)

If you try it with the other url: "http://fflffllkfl".replace(/(https?:)(\/\/.*)(jpg|gif|png)/, "lala$2$3") it won't replace anything.

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

4 Comments

How are you replacing things?
i am using freeformatter.com/regex-tester.html for now dfdkdlkdkldkldkldl.jpg i need something like this replacestringdfdkdlkdkldkldkldl.jpg
I just want to replace only http from image url
@HituBansal my code from the answer does that.. it replaces the http part for "lalala"
1

Try this:

myString.replace(/(https?:\/\/)(.*\.jpg|gif|png)/, "some string $2");

2 Comments

Please add some explanation to you answer to help both the OP and other users who use this question for reference later.
I was going to, but I was posting from my phone and by the time I got that part written I noticed that there were already several better answers. So, I figured I'd post what I had but not wast any more time when the question had already been adequately answered.

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.