Tara Irvine
Tara Irvine

Reputation: 819

can I replace everything after underscore?

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

Answers (3)

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26699

$(this).text($(this).text().replace(/_([0-9])+/, ''));

Upvotes: 1

Andy
Andy

Reputation: 30135

$(this).text($(this).text().split('_')[0]);

Upvotes: 1

keymone
keymone

Reputation: 8094

you can try this:

$(this).text($(this).text().replace(/_\d+$/, "_"));

Upvotes: 5

Related Questions