-1

I have some regex to match Imgur URLS (I think it's right, please tell me if it's not):

http://(?:i\.imgur\.com/(?.*?)\.(?:jpg|png|gif)|imgur\.com/(?:gallery/)?(?.*))$

What I want to do, is search a document for instances that match that regex, but the replace the string imgur without changing anything around it. How can I do this with Javascript?

2
  • Both of those just discuss matching URLs, rather than replacing a substring of them Commented Jun 3, 2014 at 4:29
  • FYI Added variation at the bottom to handle your revised specs. Commented Jun 3, 2014 at 6:23

1 Answer 1

1

You could use javascript's replace method.

Just use the regex \imgur\g as the parameter to replace.

See examples at http://www.w3schools.com/jsref/jsref_replace.asp. You will likely want to see the first one in the "More examples" section.

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

1 Comment

I don't want to match imgur when it is not used in the context of a URL. So for example, the sentence "Click here to visit Imgur" should not change.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.