Reputation: 819
I have some links that have _2 if they are duplicates, I want to keep the href the same but change the text to remove the _2, sometimes there's an _3 or _4 so I just want to remove from the text everything after the _.
Here is an example of the href's I want to change
<li style="display: list-item;" class="menu-item">
<div><a href="/_webapp_4002837/Dior-Charlize-Theron-2">Charlize-Theron_2</a></div>
</li>
<li style="display: list-item;" class="menu-item">
<div><a href="/_webapp_4002840/Dior-Natalie-Portman">Natalie-Portman</a></div>
</li>
<li style="display: list-item;" class="menu-item">
<div><a href="/_webapp_4002838/Dior-Sharon-Stone-4">Sharon-Stone_4</a></div>
</li>
I have already found a code that replaces the hyphens with space's and this works well, so my code so far is
$(".images li").find("a").each(function(i){
$(this).text($(this).text().replace(/-/g," "));
$(this).text($(this).text().replace(/_/g,""));//removes the _
});
Any Ideas? Thanks for replys
Upvotes: 0
Views: 1700
Reputation: 26699
$(this).text($(this).text().replace(/_([0-9])+/, ''));
Upvotes: 1
Reputation: 8094
you can try this:
$(this).text($(this).text().replace(/_\d+$/, "_"));
Upvotes: 5