0

I have sets of Images like this

http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s640/camera-541213_1920.jpg
http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s500-p/camera-541213_1920.jpg
http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s320-w160/camera-541213_1920.jpg
http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s320/camera-541213_1920.jpg

You can see image have s640, s500-p, s320-w160 which specify image height and width.

I want to replace image url (s640, s500-p, s320-w160) to (s1600) using help of regex like this

http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s1600/camera-541213_1920.jpg
http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s1600/camera-541213_1920.jpg
http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s1600/camera-541213_1920.jpg
http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s1600/camera-541213_1920.jpg

Can any tell me how can I do that

6
  • 1
    Anything you tried yet? Commented Dec 11, 2015 at 19:18
  • @TareqMahmood Yes but it didn't worked Commented Dec 11, 2015 at 19:20
  • 1
    But at least you can show us what you tried so that we can fix Commented Dec 11, 2015 at 19:24
  • Look at replace() method. Commented Dec 11, 2015 at 19:24
  • @TareqMahmood image.attr('src').replace(/s\B\d{2,4}/,'s1600') Commented Dec 11, 2015 at 19:25

2 Answers 2

1

You could make a function and split on the path segments:

// path: The image url
// replacement: The replacement string
function replaceSize(path, replacement) {
    var parts = path.split('/'); // break the string to an array
    parts[7] = replacement;      // this is the path segment to replace
    return parts.join('/');      // glue the array back into a string
}
// Test the function
console.log(replaceSize('http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s640/camera-541213_1920.jpg', 's1600'));
// output: http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s1600/camera-541213_1920.jpg
Sign up to request clarification or add additional context in comments.

Comments

0

Use JQuery:

<script type="text/javascript">
    $("img").each(function(key, val){
        var src = $(this).attr('src').replace(/s640|s500-p|s320-w160|s320/g, "s1600");
        $(this).attr('src', src);
    });
</script>

Here is the full code:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>

    <img src="http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s640/camera-541213_1920.jpg" alt="" />
    <img src="http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s500-p/camera-541213_1920.jpg" alt="" />
    <img src="http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s320-w160/camera-541213_1920.jpg" alt="" />
    <img src="http://1.bp.blogspot.com/-swqrzpzvh7s/Vi6Os2n7NGI/AAAAAAAABRs/W9uC8EJldt0/s320/camera-541213_1920.jpg" alt="" />



    <script type="text/javascript">

        $("img").each(function(key, val){
            var src = $(this).attr('src').replace(/s640|s500-p|s320-w160|s320/g, "s1600");
            $(this).attr('src', src);
        });

    </script>

</body>
</html>

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.