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

Hitu Bansal
  • 2,917
  • 10
  • 52
  • 87
  • 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. – David Thomas Nov 25 '14 at 12:16

2 Answers2

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.

Deleteman
  • 8,500
  • 6
  • 25
  • 39
1

Try this:

myString.replace(/(https?:\/\/)(.*\.jpg|gif|png)/, "some string $2");
Nathan Friedly
  • 7,837
  • 3
  • 42
  • 59
  • Please add some explanation to you answer to help both the OP and other users who use this question for reference later. – JRulle Nov 25 '14 at 13:10
  • 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. – Nathan Friedly Nov 25 '14 at 15:59