2

I trying to use .replace() to change everything between the two last slashes of this URL with "w270" but nothing happens. Am I doing something wrong? jsFiddle

<div class="post-outer">
<img src="https://1.bp.blogspot.com/-9h_QVOCOX1w/XvnuC9SucSI/AAAAAAAABJo/roFOy4Tgs64wSDtI9-dC9WyglJJdPz3fACK4BGAsYHg/w100/image.png"/>
</div>

// The RegEx target everything between the two last slashes
    $('.post-outer img').attr('src').replace(/[^\/]+(?=\/[^\/]*$)/, 'w270');

3 Answers 3

3

Replace returns a new string, it does not modify the original, you will need to assign the result back to the attribute.

const img = $('.post-outer img');
const url = img.attr('src').replace(/[^\/]+(?=\/[^\/]*$)/, 'w270');
img.attr('src', url);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="post-outer">
<img src="https://1.bp.blogspot.com/-9h_QVOCOX1w/XvnuC9SucSI/AAAAAAAABJo/roFOy4Tgs64wSDtI9-dC9WyglJJdPz3fACK4BGAsYHg/w100/image.png"/>
</div>

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

2 Comments

It works partially. I should mentioned that I have several <div class="post-outer"> within a <div class="post-list"> parent. Your code and Majed Badawi code above repeat the first .post-outer img on all following .post-outer img. I updated my fiddle with your code, can you take a look? Fiddle
You need to make your selector target the image you want to update. '.post-outer img' will target all images that are children of an element with the class 'post-outer'. $('.post-outer:first img') will target the first post. You need to explain what you want to happen.
3

Try this:

let url = $('.post-outer img').attr('src');
$($('.post-outer img')[0]).attr('src', url.replace(/[^\/]+(?=\/[^\/]*$)/, 'w270'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="post-outer">
<img src="https://1.bp.blogspot.com/-9h_QVOCOX1w/XvnuC9SucSI/AAAAAAAABJo/roFOy4Tgs64wSDtI9-dC9WyglJJdPz3fACK4BGAsYHg/w100/image.png"/>
</div>
<div class="post-outer">
<img src="https://1.bp.blogspot.com/-9h_QVOCOX1w/XvnuC9SucSI/AAAAAAAABJo/roFOy4Tgs64wSDtI9-dC9WyglJJdPz3fACK4BGAsYHg/w100/image.png"/>
</div>

6 Comments

It works partially. I should mentioned that I have several <div class="post-outer"> within a <div class="post-list"> parent. Your code and Adrian Brand code below repeat the first .post-outer img on all following .post-outer img. I updated my fiddle with Adrian code, can you take a look? Fiddle
@KimSantos just change the src for the first child as above
Majed but [0] is applying the replace only in the first <div class="post-outer"><img/></div> from a <div class="post-list"> parent with 27 <div class="post-outer"><img/></div>. Any other suggestion?
If you could take a look at my website homepage you'll see that only the first image is getting the RegEx applied with your edited code. Your first code repeat this first image on all the other images.
I mean what is missing from the solution? Don't you want to apply the replace to the first one only?
|
2

Using attr(attributeName, function) simplifies this since it exposes current value in the callback and will also loop over all matching selectors and perform same task on each instance

$('.post-outer img').attr('src', (_, curr) => curr.replace(/[^\/]+(?=\/[^\/]*$)/, 'w270'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="post-outer">
<img src="https://1.bp.blogspot.com/-9h_QVOCOX1w/XvnuC9SucSI/AAAAAAAABJo/roFOy4Tgs64wSDtI9-dC9WyglJJdPz3fACK4BGAsYHg/w100/image.png"/>
</div>
<div class="post-outer">
<img src="https://1.bp.blogspot.com/-9h_QVOCOX1w/XvnuC9SucSI/AAAAAAAABJo/roFOy4Tgs64wSDtI9-dC9WyglJJdPz3fACK4BGAsYHg/w100/image.png"/>
</div>

2 Comments

Exactly what I need. Thanks :) Can you tell me what (_, curr) => does? Googled it and couldn't find much about it.
It's an arrow function. Almost the same as function(_,curr){/* do stuff*/})). The _ is simply a placeholder argument and is the index value of the collection which is not needed in this case ... curr is the current attribute value

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.