1

So basically I have a very long url, something like this -

http://www.site.com/wp-content/themes/theme/timthumb.php?src=/wp-content/uploads/2012/random-image-name.jpg&w=650&h=0&zc=1&q=100

and I would need it to automatically change the link to this -

http://www.site.com/wp-content/uploads/2012/random-image-name.jpg

I know it's doable with Javascript Preg Match, but I'm not familiar with preg match, so I'm not sure how to create that.

Could you help me craete the link?

There will be a post iwth more than 200 images in it, and I need to do it for each of the image, so best solution woudl be if it would do it for all images at once.

1
  • so these links are actually the src attribute of <img> tags? Commented Jul 3, 2012 at 8:53

2 Answers 2

2

Working demo this should suffice: http://jsfiddle.net/jdAxX/1/

Hope this helps, :) all it does is that it takes the url indexOf from ? and then split your src and use it as you require.

code

var value = "http://www.site.com/wp-content/themes/theme/timthumb.php?src=/wp-content/uploads/2012/random-image-name.jpg&w=650&h=0&zc=1&q=100";
var returnStr = value.substr(value.indexOf("?") + 1);
var spliMe = returnStr.split('&');
var newURL = "http://www.site.com"+spliMe[0].split("=")[1];
Sign up to request clarification or add additional context in comments.

6 Comments

@user1446087 are these links in <img> tags?
@Alnitak Ah bruv, please guide me I will make it right, any suggestion will be taken seriously, cheers for comment and advise, :) (its about the comment you deleted) thnx
@Tats_innit I'd suggest formatting your code correctly, and losing the jsfiddle screen shot - it's illegible and adds nothing to the answer.
@Alnitak Thnx; is this good bruv? thanx for suggestion again! I really appreciate your time! :)
@Tats_innit get rid of the document.ready function too. You've also now missed out the final step
|
0

You're mixing languages there. preg_match is PHP, nothing to do with JavaScript.

In JS you have various methods for using regular expressions, namely test, match, search and exec (and others). I suggest you start at http://www.regular-expressions.info for a general overview of regex and then follow with this article https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions.

Try on your own first, if you got any problems come back with some code and people will be able to help.

Comments

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.